Android中的网络超时

我在我的应用程序和服务器之间的通信中经历了太多的超时.

我的问题是服务器甚至没有收到消息.所以我猜问题是在我的
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

谢谢你的帮助!
奥马里

最佳答案 好吧,我不确定是什么解决了它,但我认为问题是我没有关闭

无论如何,这里的连接和方法是我的代码,如果有人感兴趣:

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;
}
点赞