Results 1 to 3 of 3
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    287
    Rep Power
    3

    Default How to set timeout for DefaultHttpClient

    Hi!

    I have try to set timeout for the below "GET" method but receiving problems..,

    Can You please guide me how to set the timeout for the below "GET" method..,


    Java Code:
    
    package dcs.raj.cp.helper;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.util.List;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.impl.client.DefaultHttpClient;
    
    import android.util.Log;
    
    public class JSONParser {
    
    	static InputStream is = null;
    	static String json = "";
    
    	public JSONParser() {
    
    	}
    
    	public String makeHttpRequest(String url, String method,
    			List<NameValuePair> params) {
    		try {
    			if (method == "POST") {
    				DefaultHttpClient httpClient = new DefaultHttpClient();
    				HttpPost httpPost = new HttpPost(url);
    				httpPost.setEntity(new UrlEncodedFormEntity(params));
    
    				HttpResponse httpResponse = httpClient.execute(httpPost);
    				HttpEntity httpEntity = httpResponse.getEntity();
    				is = httpEntity.getContent();
    
    			} else if (method == "GET") {
    
    				DefaultHttpClient httpClient = new DefaultHttpClient();
    				String paramString = URLEncodedUtils.format(params, "utf-8");
    				url += "?" + paramString;
    				HttpGet httpGet = new HttpGet(url);
    
    				HttpResponse httpResponse = httpClient.execute(httpGet);
    				HttpEntity httpEntity = httpResponse.getEntity();
    				is = httpEntity.getContent();
    			}
    
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		} catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(
    					is, "iso-8859-1"), 8);
    			StringBuilder sb = new StringBuilder();
    			String line = null;
    			while ((line = reader.readLine()) != null) {
    				sb.append(line + "\n");
    			}
    			is.close();
    			json = sb.toString();
    		} catch (Exception e) {
    			Log.e("Buffer Error", "Error converting result " + e.toString());
    		}
    		return json;
    	}
    }
    Thank You!

    Have A Happy Day..,
    - Raj

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to set timeout for DefaultHttpClient

    First Google hit for "defaulthttpclient timeout": Timeout in DefaultHttpClient Class Android - Stack Overflow

    BTW, using DefaultHttpClient is not recommended. See DefaultHttpClient | Android Developers
    Get in the habit of using standard Java naming conventions!

  3. #3
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    287
    Rep Power
    3

    Default Re: How to set timeout for DefaultHttpClient

    Thanks Mr.kjkrum i will try as your idea..,

    and i will fallow the standard Java naming conventions too,

    Thank You..,
    Last edited by raj.mscking@gmail.com; 10-04-2013 at 06:46 AM.
    - Raj

Similar Threads

  1. Timeout!
    By NoobieCode in forum New To Java
    Replies: 1
    Last Post: 05-08-2013, 08:15 AM
  2. Timeout db2
    By nicox1984 in forum New To Java
    Replies: 17
    Last Post: 01-20-2012, 09:53 AM
  3. Replies: 1
    Last Post: 11-29-2011, 01:30 PM
  4. JAR server timeout
    By Axephilic in forum Advanced Java
    Replies: 0
    Last Post: 10-24-2011, 07:22 PM
  5. AndroidHttpClient and DefaultHttpClient
    By divakarnvs in forum Android
    Replies: 2
    Last Post: 09-13-2010, 10:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •