這裏我有一個在Android中獲取請求的問題。 我在我的電腦上運行了使用webapp2的服務器程序,併成功地將請求從本身發送到服務器程序。 但是,當我嘗試從手機發送請求時,程序總是跳轉catch(Exception e)部分。這三個Log.e()工作正常,因爲我可以在日誌記錄中看到它們。誰能告訴我這裏可能會出現什麼問題?我的電腦用來阻止請求的防火牆是什麼?提前致謝。 PS:變量resultString是我從語音識別中獲得的結果。關於android發送獲取請求
try{
StringBuilder buf = new StringBuilder("http://192.168.1.100:9080/");
buf.append("?");
buf.append("content1="+URLEncoder.encode(resultString,"UTF-8"));
//buf.append(resultString);
//Toast.makeText(MainActivity.this, buf, Toast.LENGTH_SHORT).show();
URL url = new URL(buf.toString());
Log.e("check1","url_transformation works ok");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Log.e("check2","open connection works ok");
conn.setRequestMethod("GET");
Log.e("check3","set get works ok");
if(conn.getResponseCode()==200){
Log.e("check4","get works");
Toast.makeText(MainActivity.this, "GET works", Toast.LENGTH_SHORT).show();
}
else
{
Log.e("check5","get fails");
Toast.makeText(MainActivity.this, "GET fails", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
Toast.makeText(MainActivity.this, "other problems occur", Toast.LENGTH_SHORT).show();
}
03-05 20:47:19.486:W/System.err的(16066):android.os.NetworkOnMainThreadException 03-05 20:47:19.486:W/System.err的(16066):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) 03-05 20:47:19.486:W/System.err的(16066):在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84 ) 03-05 20:47:19.486:W/System.err的(16066):在libcore.io.IoBridge.connectErrno(IoBridge.java:127) 03-05 20:47:19.486:W/System.err的(16066):在libcore.io.IoBridge.connect(IoBridge.java:112) 03-05 20:47:19.486:W/System.err的(16066):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java :192) 03-05 20:47:19.486:W/System.err的(16066):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 03-05 20:47:19.486:W/System.err的( 16066):在java.net.Socket.connect(Socket.java:872) 03-05 20:47:19.486:W/System.err(16066):在libcore.net.http.HttpConnection。(HttpConnection.java :77)
我認爲異常日誌如上所述。那麼這裏有什麼問題?
我試過AsyncTask,因爲你們有些人建議。 該代碼是作爲講座敬愛:
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);
}
}
03-06 14:57:07.117:W/System.err的(12451):org.apache.http.conn.ConnectTimeoutException:連接到/192.168.1.100:9080超時 03-06 14:57:07.137:W/System.err的(12451):在org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121) 03-06 14點57: 07.137:W/System.err的(12451):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
日誌示出了有一個IOException的在我的代碼和Ť他的mClient價值將最終爲空。我在網上查找了相同的情況,看起來這發生在數據花費太多時間來響應請求時發生。許多答案建議我應該重置超時並允許更多時間發送請求。但我真的不明白在哪裏把重置代碼放在我的情況中。有人能幫忙嗎?謝謝。
請不要使用粗體文字descripe您的問題... –
你應該嘗試捕捉異常,並告訴我們它說什麼。順便說一句,你是否將android.permission.INTERNET權限添加到清單中? – eugecm
是的,我確實添加了許可,因爲識別部分也需要它。通過捕獲異常你的意思是什麼?打印出e的內容? – lonelyjohner