How to Set Httpresponse Timeout For Android in Java

How to set HttpResponse timeout for Android in Java

In my example, two timeouts are set. The connection timeout throws java.net.SocketTimeoutException: Socket is not connected and the socket timeout java.net.SocketTimeoutException: The operation timed out.

HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

If you want to set the Parameters of any existing HTTPClient (e.g. DefaultHttpClient or AndroidHttpClient) you can use the function setParams().

httpClient.setParams(httpParameters);

Android: AndroidHttpClient - how to set timeout?

I did miss to attach the params to my http request, but the proper way to do this in my example is

httpGet.setParams(httpParams);

before calling httpClient.execute(httpGet).

Just added that line and it worked fine.

Android HttpPost Request Timeout

Its not working. I am using the following class:

public class ConnectionManager {

private ArrayList <NameValuePair> params;
private ArrayList <NameValuePair> headers;
private String url;

public ConnectionManager(String url) {
this.url = url;
params = new ArrayList<NameValuePair>();
headers = new ArrayList<NameValuePair>();
}

public void addParam(String name, String value)
{
params.add(new BasicNameValuePair(name, value));
}

public void addHeader(String name, String value)
{
headers.add(new BasicNameValuePair(name, value));
}

public String sendRequest() throws Exception {
String serverResponse = "";
HttpPost httpPostRequest = new HttpPost(url);
httpPostRequest.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
//add headers
for(int i = 0; i < headers.size(); i++) {
StringEntity entity = new StringEntity(headers.get(i).getValue(),"UTF-8");
httpPostRequest.setEntity(entity);
}

if(!params.isEmpty()){
HttpEntity httpEntity = new UrlEncodedFormEntity(params,HTTP.UTF_8);
httpPostRequest.setEntity(httpEntity);
}

serverResponse = executeRequest(httpPostRequest);
return serverResponse;
}

private String executeRequest(HttpUriRequest request) throws Exception {

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 10000);
DefaultHttpClient client = new DefaultHttpClient(params);

HttpResponse httpResponse;
String serverResponse = "";
httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
serverResponse = convertStreamToString(instream);
instream.close();
}
Log.d("server response", serverResponse);
return serverResponse;
}

private String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

how to implement request timeout in android?

Set up your HttpClient this way.

    BasicHttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, connectionTimeoutInMs);
httpClient = new DefaultHttpClient(httpParams);

how to set Http connection timeout on Android

Try this ,private static long TIME_OUT_IN_SECONDS = 120;

          System.out.println("posthhhhhhhhhhhhhhhhhhh");

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = null;
long requestStratTime = new Date().getTime();

httpResponse = httpClient.execute(httpPost);
long requestEndTime = new Date().getTime();
Log.d("requestStratTime", "requestStratTime" + requestStratTime);
Log.d("requestEndTime", "requestEndTime" + requestEndTime);
long timeOfRequest = (requestEndTime - requestStratTime) / 1000;
Log.d("timeOfRequest", "timeOfRequest" + timeOfRequest);
if (httpResponse == null && timeOfRequest > TIME_OUT_IN_SECONDS) {

throw new TimeOutException();
}

int responseCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("responseCode" + responseCode);
String ss = httpResponse.getStatusLine().toString();
System.out.println("ssssssssssssssssssssssss" + ss);
if (responseCode == 200) {

HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} else {
throw new ParsingException();
}

Android Application Timeout even after AsyncHttpClient timeout set

In the end, managed to figure out all the different error scenarios just combinations of error.getMessage(), error.toString() and error.getCause()

Toast.makeText(getApplicationContext(), "Status code :"+statusCode +"errmsg : "+error.getMessage(), Toast.LENGTH_LONG).show();
Currently you are displaying custom message ,First check which error status code and error content,There are many status codes you handle only two cases



Related Topics



Leave a reply



Submit