I am experiencing way too many timeouts in the communication between my app and my server..
My problem is that the server doesn’t even get the message. So I guess the problem is somewhere inside my Android code and not the server code.
Here is my code:
HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
//configure timeouts
HttpConnectionParams.setConnectionTimeout(params, 1000 * 1000);
HttpConnectionParams.setSoTimeout(params, 1000 * 1000);
//and finally initialize the http client
this.mClient = new DefaultHttpClient(manager,params);
//init the response handler
this.mResponseHandler = new MyResponseHandler(ctx);
final HttpPost method = new HttpPost(HttpSender.SERVER_URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("message", s));
try {
method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Runnable sender = new Runnable(){
@Override
public void run() {
try {
mClient.execute(method, mResponseHandler);
} catch (ClientProtocolException e) {
Log.v(TAG, "unable to send " + s);
e.printStackTrace();
} catch (IOException e) {
Log.v(TAG, "unable to send " + s);
e.printStackTrace();
}
}
};
new Thread(sender).start();
Is there something wrong with my code? By the way it doesn’t always timeout.. just about 2/3 of the time
EDIT : I noticed that when i start the phone it usually works fine (no timeouts) but after
a few messages i start getting the timeouts so maybe i am not closing something correctly
in my code?
EDIT : another thing i have noticed is that if the messages work fine and then i turn my phone (from vertical to horizontal and vice versa)
the problems arise again and right after the orientation change i get:
10-03 11:47:46.920: ERROR/[FT]-Server(3563): NetworkStateReceiver :intent: android.net.conn.CONNECTIVITY_CHANGE
Thanks for your help!
Omri
well i am not exactly sure what solved it but i think the problem was that i wasn’t closing
connection and methods anyhow here is my code if any one is interested: