在尝试向PC服务器发送http请求时,我尝试过AsyncTask。 创建变量resultString并分配语音识别结果。这部分之前的代码工作正常。
有问题的代码如下:
private class HttpGetTask extends AsyncTask<Void , Void, String> {
private String URL = "http://192.168.1.100:9080/" + "?" +"content1="+resultString;
//buf.append("content1="+URLEncoder.encode(resultString,"UTF-8"));
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
//Log.e("check", "succeed");
@Override
protected String doInBackground(Void... param) {
HttpGet request = new HttpGet(URL);
//Log.e("check1","succeed");
Log.e("url",URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
Log.e("check3","succeed");
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
Log.e("check4","succeed");
} catch (IOException exception) {
exception.printStackTrace();
Log.e("check5","succeed");
}
Log.e("check6","succeed");//to this point
return null;
}
@Override
protected void onPostExecute(String result) {
if (null != mClient)
mClient.close();
//Log.e("check6","succeed");
mTextView.setText(result);
}
}
执行部分在整个代码的末尾看起来像这样:
new HttpGetTask().execute();
logcat的
03-06 14:57:07.117: W/System.err(12451): org.apache.http.conn.ConnectTimeoutException:
Connect to /192.168.1.100:9080 timed out 03-06 14:57:07.137: W/System.err(12451):
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
03-06 14:57:07.137: W/System.err(12451): at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
日志显示我的代码中有IOException
,mClient
的值最终将为null
。我在线查看了相同的情况,看起来这种情况发生在数据需要太多时间来响应请求。许多答案表明我应该重置超时并允许更多时间发送请求。(也许网络不可靠......)但我真的不明白在我的情况下将重置代码放在哪里。有人能帮忙吗?感谢。
答案 0 :(得分:0)
您需要检查您的网址: -
http://192.168.1.100:9080/
工作正常与否。 ConnectTimeoutException是一个例外,它指出了URL根本无法访问的事实。