我在我的应用和服务器之间的通信中经历了太多超时..
我的问题是服务器甚至没有收到消息。所以我猜问题是在我的Android代码中,而不是服务器代码。
这是我的代码:
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();
我的代码有问题吗?顺便说一下,它并不总是超时...只有大约2/3的时间
编辑:我注意到,当我启动手机时,它通常可以正常工作(没有超时)但是在执行后 一些消息,我开始得到超时,所以也许我没有正确关闭一些东西 在我的代码中?
编辑:我注意到的另一件事是,如果消息正常,然后我转动手机(从垂直到水平,反之亦然)10-03 11:47:46.920: ERROR/[FT]-Server(3563): NetworkStateReceiver :intent: android.net.conn.CONNECTIVITY_CHANGE
感谢您的帮助!
欧米
答案 0 :(得分:2)
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;
}