我正在尝试使用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