当Android通过WiFly与Arduino通信时,HTTP失败

时间:2012-12-08 07:37:58

标签: android http-post arduino httpresponse http-get

我正在尝试使用HTTP将命令传递给我的Arduino设备。设备接收命令,并按预期执行。但我在HttpClient.execute()中收到错误,如下所述。我如何更正代码?

protected String doInBackground(Void... params) {
    try {
        Log.i("CallArduino ","In doInBackground");
        final int TIMEOUT_MILLISEC = 1000;
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);
        //    URL="http://www.google.co.in";
        HttpGet get = new HttpGet(URL);

        Log.i("CallArduino ","before get");
                        responseGet = client.execute(get);
        Log.i("CallArduino ","after get");
        resEntityGet = responseGet.getEntity();

        Log.i("CallArduino ","in PostExecute");
        if (resEntityGet != null) {
            InputStream is = resEntityGet.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
        }
    }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{

    }

输出:

 12-08 12:58:10.419: I/CallArduino(5652): before get
 12-08 12:58:11.589: W/System.err(5652): java.net.SocketTimeoutException
 12-08 12:58:11.589: W/System.err(5652): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
 12-08 12:58:11.589: W/System.err(5652): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
 12-08 12:58:11.589: W/System.err(5652): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.io.AbstractMessageParser.parseHeaders(AbstractMessageParser.java:122)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:178)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
 12-08 12:58:11.599: W/System.err(5652): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
 12-08 12:58:11.609: W/System.err(5652): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
 12-08 12:58:11.609: W/System.err(5652): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
 12-08 12:58:11.609: W/System.err(5652): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
 12-08 12:58:11.609: W/System.err(5652): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
 12-08 12:58:11.609: W/System.err(5652): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512)
 12-08 12:58:11.609: W/System.err(5652): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490)
 12-08 12:58:11.609: W/System.err(5652): at com.example.homeautomation.CallArduino$httpTask.doInBackground(CallArduino.java:87)
 12-08 12:58:11.609: W/System.err(5652): at com.example.homeautomation.CallArduino$httpTask.doInBackground(CallArduino.java:1)
 12-08 12:58:11.609: W/System.err(5652): at android.os.AsyncTask$2.call(AsyncTask.java:264)
 12-08 12:58:11.609: W/System.err(5652): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 12-08 12:58:11.609: W/System.err(5652): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 12-08 12:58:11.609: W/System.err(5652): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
 12-08 12:58:11.609: W/System.err(5652): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 12-08 12:58:11.609: W/System.err(5652): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 12-08 12:58:11.619: W/System.err(5652): at java.lang.Thread.run(Thread.java:856)
 12-08 12:58:11.619: I/CallArduino(5652): result  :null

1 个答案:

答案 0 :(得分:0)

您的互联网连接速度可能很弱。您可以使用以下方法增加超时间隔。

HttpConnectionParams.setSoTimeout(params, 1000);