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 ph开发者_如何学运维one (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!
Omriwell 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:
public class HttpSender {
private ResponseHandler<String> mResponseHandler;
private String TAG = "HttpSender";
static final String SERVER_URL = "http://**some ip address**/myServer";
public HttpSender(Context ctx){
//init the response handler
this.mResponseHandler = new MyResponseHandler(ctx);
}
//TODO : see if using postSend is better and dosent cause timeouts?
public void send(final String s,final Context ctx, final int type){
Log.v(TAG, "sending message : " + s + "type message of is " + Integer.toString(type));
//for testing
Toast.makeText(ctx,"sending \n" + s,Toast.LENGTH_LONG).show();
//done for testing
this.postSend(s, ctx, type);
}
public void postSend(final String s,final Context ctx,final int type){
final HttpPost method = new HttpPost(HttpSender.SERVER_URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("message", s));
final DefaultHttpClient client = getNewClient(ctx);
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 {
client.execute(method, mResponseHandler, new BasicHttpContext());
client.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
method.abort();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
method.abort();
Log.v(TAG, "unable to send " + s);
e.printStackTrace();
}
}
}
};
new Thread(sender).start();
}
private DefaultHttpClient getNewClient(Context ctx) {
HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
//and finally initialize the http client
DefaultHttpClient client = new DefaultHttpClient(manager,params);
return client;
}
精彩评论