Network timeouts in Android
<p>I am experiencing way too many timeouts in the communication between my app and my server..<br>
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.  </p>

<p>Here is my code:  </p>

<pre><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&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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();
</code></pre>

<p>Is there something wrong with my code? By the way it doesn't always timeout.. just about 2/3 of the time</p>

<p>EDIT : I noticed that when i start the phone it usually works fine (no timeouts) but after<br>
a few messages i start getting the timeouts so maybe i am not closing something correctly<br>
in my code?</p>

<p>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)<br>
the problems arise again and right after the orientation change i get:</p>

<pre><code>10-03 11:47:46.920: ERROR/[FT]-Server(3563): NetworkStateReceiver :intent: android.net.conn.CONNECTIVITY_CHANGE
</code></pre>

<p>Thanks for your help!<br>
Omri</p>
