2

I have been testing a small embedded web server, Web server, (it is running mike G.'s multisocket server). I am trying to get get a response from the server using the android virtual devices in eclipse and for some reason the server fails to respond half the time.

here is the logcat of the error. "on" is the string the server will respond with when successful.

09-03 23:12:54.672: I/System.out(996): on
09-03 23:12:55.989: I/System.out(996): on
09-03 23:12:57.113: D/dalvikvm(996): GC_FOR_MALLOC freed 882K, 53% free 3142K/6663K,           external 1625K/2137K, paused 37ms
09-03 23:12:57.113: W/System.err(996): org.apache.http.NoHttpResponseException: The target server failed to respond
09-03 23:12:57.122: W/System.err(996):  at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
09-03 23:12:57.122: W/System.err(996):  at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
09-03 23:12:57.122: W/System.err(996):  at   org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
09-03 23:12:57.122: W/System.err(996):  at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
09-03 23:12:57.122: W/System.err(996):  at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
09-03 23:12:57.132: W/System.err(996):  at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
09-03 23:12:57.132: W/System.err(996):  at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
09-03 23:12:57.132: W/System.err(996):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
09-03 23:12:57.132: W/System.err(996):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-03 23:12:57.132: W/System.err(996):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-03 23:12:57.132: W/System.err(996):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-03 23:12:57.132: W/System.err(996):  at com.SS.Main.TestHttpGet.executeHttpGet(TestHttpGet.java:40)
09-03 23:12:57.132: W/System.err(996):  at com.SS.Main.SmartHomeSystemActivity$1.onClick(SmartHomeSystemActivity.java:32)
09-03 23:12:57.132: W/System.err(996):  at android.view.View.performClick(View.java:2485)
09-03 23:12:57.132: W/System.err(996):  at android.view.View$PerformClick.run(View.java:9080)
09-03 23:12:57.142: W/System.err(996):  at android.os.Handler.handleCallback(Handler.java:587)
09-03 23:12:57.142: W/System.err(996):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 23:12:57.142: W/System.err(996):  at android.os.Looper.loop(Looper.java:123)
09-03 23:12:57.142: W/System.err(996):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-03 23:12:57.142: W/System.err(996):  at java.lang.reflect.Method.invokeNative(Native Method)
09-03 23:12:57.142: W/System.err(996):  at java.lang.reflect.Method.invoke(Method.java:507)
09-03 23:12:57.142: W/System.err(996):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-03 23:12:57.142: W/System.err(996):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-03 23:12:57.152: W/System.err(996):  at dalvik.system.NativeStart.main(Native Method)

I tried to make a retry handler from this thread

retry handler

but that didnt seem to fix it (or i was using it incorrectly) so i decided to try to just keep re-sending the request. Here is the code that is working, but i feel it is not a good way to do it. The server has no problems when a browser from a PC sends the request, but will fail to respond sometimes with android browsers and the application emulation. any ideas?

 package com.SS.Main;

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URI;

 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.NoHttpResponseException;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.HttpRequestRetryHandler;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.AbstractHttpClient;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;

 public class TestHttpGet {

 public void executeHttpGet() throws Exception {

    BufferedReader in = null;

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();

        HttpProtocolParams.setUseExpectContinue(client.getParams(), false);


        request.setURI(new URI("http://142.197.135.16:5000/aled.htm?led=on"));

        //HttpResponse response = client.execute(request);

        HttpResponse response = null;

        boolean RetryConnection = true;
        int retrycount = 0;

        //retry request 5 times
        while(RetryConnection == true && retrycount < 5)
        {
            try{
                 response = client.execute(request);
                 RetryConnection = false;
            }catch(IOException e)
            {
                System.out.println("caught");
                retrycount += 1;
            }
        }



        in = new BufferedReader
        (new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");

        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String page = sb.toString();

        System.out.println(page);
        } finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}
Community
  • 1
  • 1
dmoody256
  • 538
  • 4
  • 12
  • Can you try this and post back result? httpResponse.getStatusLine().getStatusCode() – Infinity Sep 03 '12 at 23:49
  • i printed it to the console and when it is successful it prints "200" then "on". Is there a way i can read the code when the server fails to respond? – dmoody256 Sep 04 '12 at 00:02
  • You may need to use `.addHeader`. Take a look at my answer [here](http://stackoverflow.com/questions/11526437/android-illegalstateexception-in-httpget/11526693#11526693) – Ali Sep 04 '12 at 00:16

1 Answers1

0

turns out it was the server that was the problem. The way the server was setup it was not getting enough time to process the incoming request. The solution to the problem can be found in this link.

Spinneret web server fix

dmoody256
  • 538
  • 4
  • 12