DefaultHttpClient causing delay in async task - java

I am developing an app which send request to server using DefaultHttpClient.I am sending request with GET method.if the port number is wrong then i get connection timeout exception.I have used handler for the exception & return a value when exception occurs. But the sad part is Post Execute is called after 60 seconds.
protected String doInBackground(String... url)
{
HttpURLConnection con = null;
HttpGet httpGet = null;
DefaultHttpClient httpClient=null;
try
{
httpGet = new HttpGet(url[0]);
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 5000;
int sockettimeout=5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, sockettimeout);
httpClient = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse = httpClient.execute(httpGet );
int code=httpResponse.getStatusLine().getStatusCode();
}
catch (IOException e)
{
if(e.toString().contains("org.apache.http.conn.ConnectTimeoutException"))
{
Log.i("RAE", "Connection Timeedout");
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().shutdown();
httpGet.abort();
Log.i("log", "Before Return");
return "done";
}
#Override
protected void onPostExecute(String result) {
Log.i("log", "Async finished");
}
But post is called after 60 seconds.May i know why it is happening

Related

Android AsyncTask, handle unavailable server

I am using this tutorial:
http://hmkcode.com/android-parsing-json-data/ to get JSON data from server on virtual machine. It works fine, when server is turned on, but my app crashes when server is unavailable.
How should I handle this situation?
After pressing button I execute:
httpAsyncTask.execute("http://localhost:3000"); //changed on purpose, it works with given IP
httpAsyncTask class:
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
#Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Worked fine", Toast.LENGTH_LONG).show();
goToMyActivity();
}
}
In debug my app stops at:
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
int GET method:
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e){
Log.e("Http:",e.toString());
}
return result;
}
Caught exception was null.
use getResponseCode(); of HttpURLConnection class to make sure that the connection is established successfully.
getResponseCode(); would return 200 if the connection is successful.
int responseCode = -1;
feedURL = "your URL"
HttpURLConnection connection;
connection = (HttpURLConnection) feedURL.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
then use if block to check if 200 is returned by connection.getResponseCode().
the constant HttpURLConnection.HTTP_OK can be used instead of hard coding 200.
if (responseCode == HttpURLConnection.HTTP_OK) {
// do your stuff here
}
else {
// Show some error dialog or Toast message
}
Use the catch block to catch the timeout exception (and others):
String message = "";
try {
//HTTP stuff here
if (responseCode == HttpURLConnection.HTTP_OK) {
// do your stuff here
} else {
// Show some error dialog or Toast message
}
} catch (Exception e) {
message = e.getLocalizedMessage();
e.printStackTrace();
}
// Toast message
This function will return null if it gets timeout otherwise will send the response
public static String Get(String url) {
// Create a new HttpClient
HttpParams params = new BasicHttpParams();
// set your timeouts here
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 10000);
HttpClient httpclient = new DefaultHttpClient(params);
try {
// Execute HTTP GET Request
HttpResponse response = httpclient.execute(new HttpGet(url));
return EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

Android httpclient login to Rails server

I started to learn about how to make an Android application. I tried to connect my app to rails server by using httpclient, however I cannot understand how to connect between app and the remote server.
Here is part of my code, and I matched id form inside "BasicNameValuePair" with html id values. Please let me know how to check whether login is successful or not.
class SendPost extends AsyncTask<Void, Void, String>
{
protected String doInBackground(Void... unused) {
String content = executeClient();
return content;
}
protected void onPostExecute(String result) {
}
public String executeClient() {
ArrayList<NameValuePair> post = new ArrayList<NameValuePair>();
post.add(new BasicNameValuePair("user_name", "SallyCook"));
post.add(new BasicNameValuePair("user_email", "domain#ppls.kr"));
post.add(new BasicNameValuePair("user_password", "add123456"));
post.add(new BasicNameValuePair("user_password_confirmation", "add123456"));
post.add(new BasicNameValuePair("user_phone", "01013089579"));
HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
System.out.println(params);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
HttpPost httpPost = new HttpPost("http://www.ppls.kr/users/sign_up");
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(post, "UTF-8");
httpPost.setEntity(entity);
HttpResponse responsePost = client.execute(httpPost);
System.out.println(responsePost.getStatusLine());
HttpEntity resEntity=responsePost.getEntity();
if (resEntity != null) {
Log.w("RESPONSE", EntityUtils.toString(resEntity));
}
return EntityUtils.getContentCharSet(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

HttpResponse & Timeout for Android

I want my device to give up the http connexion after 5 seconds.
But my code does not work... I never get any timeout message when shutting network down.
Just like if the device still tries to connect, despite de timeout...
Have an idea?
Am I trying to catch the right exception?
Thanks.
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() < 400)
{
... //data processing
}
else
{
errorMsgId = R.string.http_site_error;
}
}
catch (ConnectTimeoutException e)
{
Toast.makeText(this, "Network timeout reached!", Toast.LENGTH_SHORT).show();
Log.e("+++++++++++++++++ ","Network timeout reached!");
}
ok, GOT IT, so in case this could help someone else:
HttpClient httpclient = new DefaultHttpClient();
final HttpParams httpParams = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
You can use something like this:
/**
* Check availability of web service
*
* #param host Address of host
* #param seconds Timeout in seconds
* #return Availability of host
*/
public static boolean checkIfURLExists(String host, int seconds)
{
HttpURLConnection httpUrlConn;
try
{
httpUrlConn = (HttpURLConnection) new URL(host).openConnection();
// Set timeouts in milliseconds
httpUrlConn.setConnectTimeout(seconds * 1000);
httpUrlConn.setReadTimeout(seconds * 1000);
// Print HTTP status code/message for your information.
System.out.println("Response Code: " + httpUrlConn.getResponseCode());
System.out.println("Response Message: "
+ httpUrlConn.getResponseMessage());
return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
return false;
}
}
Perhaps I'm missing something, but where are you associating the parameters that you set the timeout in with the HttpClient that you have created? Shouldn't you do something like this:
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
...
HttpClient httpclient = new DefaultHttpClient(httpParameters);

Try Internet Connectivity only for few Seconds

I am trying to connect to internet where I have to fetch data, if the time exceeds more than 5 secs to connect I have to finish the process & continue to work offline.
Everything is working fine, sometimes it takes around 10secs to return when internet is not available, Now I have to return the xml == null; when the time exceeds more than time limit,
I don't want to do this in Async Task
public String getUrlData(String url) {
String xml = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
System.out.println("waiting");
HttpResponse httpResponse;
try {
// start the timer here
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
// check if the timer has exceeded by "if else"
// move to "return xml;" Manually when exceeds 5sec, but how?
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
}
Edited Code after this answer
public String getUrlData(String url) {
String xml = null;
final int TIMEOUT_MILLISEC = 5000; // 5 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
System.out.println("waiting");
HttpResponse httpResponse;
try {
// start the timer here
System.out.println("Started");
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Ended");
return xml;
}
LogCat Here >> 20 Secs
All you need to do is to define a timeout limit for your connections. For example:
final int TIMEOUT_MILLISEC = 5000; // 5 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient httpClient = new DefaultHttpClient(httpParams);
and afterwards, use httpClient in the same way you are using it.
Edit
public String getUrlData(String url) {
String xml = null;
final int TIMEOUT_MILLISEC = 5000; // 5 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
System.out.println("waiting");
HttpResponse httpResponse;
try {
// start the timer here
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
// check if the timer has exceeded by "if else"
// move to "return xml;" Manually when exceeds 5sec, but how?
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
}
How about this:
Thread thread = new Thread() {
#Override
public void run() {
// do the downloading thing..
}
};
thread.start();
thread.join(5000);
This is just an idea, but you could set up a delayed runnable and check after 5 seconds if the file has any size.
HttpParams params = new BasicHttpParams();
// Turn off stale checking. Our connections break all the time anyway,
// and it's not worth it to pay the penalty of checking every time.
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
HttpConnectionParams.setSocketBufferSize(params, 8192);
// Don't handle redirects -- return them to the caller. Our code
// often wants to re-POST after a redirect, which we must do ourselves.
HttpClientParams.setRedirecting(params, false);
// Use a session cache for SSL sockets
SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);
// Set the specified user agent and register standard protocols.
HttpProtocolParams.setUserAgent(params, userAgent);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https",
SSLCertificateSocketFactory.getHttpSocketFactory(
SOCKET_OPERATION_TIMEOUT, sessionCache), 443));
ClientConnectionManager manager =
new ThreadSafeClientConnManager(params, schemeRegistry);
// We use a factory method to modify superclass initialization
// parameters without the funny call-a-static-method dance.
return new AndroidHttpClient(manager, params);
Change SOCKET_OPERATION_TIMEOUT value according to your need.
This code may help you
this code from last answer is Ok
final int TIMEOUT_MILLISEC = 5000; // 5 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient httpClient = new DefaultHttpClient(httpParams);
Only when you are trying to execute your request you have to care about 3 more exceptions
SocketTimeoutException, UnknownHostException, ConnectTimeoutException
so catch this 3 exceptions also.

Android: send post that has no response

In my app, I need to send all sorts of POST requests to a server. some of those requests have responses and others don't.
this is the code I'm using to send the requests:
private static final String TAG = "Server";
private static final String PATH = "http://10.0.0.2:8001/data_connection";
private static HttpResponse response = null;
private static StringEntity se = null;
private static HttpClient client;
private static HttpPost post = null;
public static String actionKey = null;
public static JSONObject sendRequest(JSONObject req) {
try {
client = new DefaultHttpClient();
actionKey = req.getString("actionKey");
se = new StringEntity(req.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, "application/json"));
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post = new HttpPost(PATH);
post.setEntity(se);
Log.d(TAG, "http request is being sent");
response = client.execute(post);
Log.d(TAG, "http request was sent");
if (response != null) {
InputStream in = response.getEntity().getContent();
String a = convertFromInputStream(in);
in.close();
return new JSONObject(a);
}
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "encoding request to String entity faild!");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d(TAG, "executing the http POST didn't work");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "executing the http POST didn't work");
e.printStackTrace();
} catch (JSONException e) {
Log.d(TAG, "no ActionKey");
e.printStackTrace();
}
return null;
}
private static String convertFromInputStream(InputStream in)
throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return (sb.toString());
}
This is the code for the AsyncTask class that sends the request:
class ServerRequest extends AsyncTask<JSONObject, Void, JSONObject> {
#Override
protected JSONObject doInBackground(JSONObject... params) {
JSONObject req = params[0];
JSONObject response = Server.sendRequest(req);
return response;
}
#Override
protected void onPostExecute(JSONObject result) {
// HANDLE RESULT
super.onPostExecute(result);
}
}
my problem starts when the server doesn't return a response. the AsyncTask thread stays open even after the work is done because the HTTPClient never closes the connection.
Is there a way to not wait for a response? this is something that will definitely add a lot of overhead to the server since all the Android apps trying to connect to it will keep the connection alive, and will probably cause many problems on the app itself.
Basically, what I'm looking for is a method that will allow me to send to POST message and kill the connection right after the sending of the request since there is no response coming my way.
Just, Set ConnectionTimeOut with HttpClient Object, (Code is for your understanding in your case it may be different)
int TIMEOUT_MILLISEC = 30000;
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/json");
Now, It will terminate the Connection after TimeoOut you defined. But be sure this will throw TimeOutException so You have to handle this exception in your HttpRequest.. (Use Try -catch)
EDIT: Or you can use HttpRequestExecutor class.
From class HttpRequestExecutor of package org.apache.http.protocol
protected boolean canResponseHaveBody (HttpRequest request, HttpResponse response)
Decide whether a response comes with an entity. The implementation in this class is based on RFC 2616. Unknown methods and response codes are supposed to indicate responses with an entity.
Derived executors can override this method to handle methods and response codes not specified in RFC 2616.

Categories