所以,當我嘗試運行下面的代碼,我永遠不會通過響應線。我究竟做錯了什麼?我已在我的清單文件中允許訪問互聯網。我一定會錯過一些巨大的東西,因爲我看過大約100個與我的代碼完全相同的類似示例。我甚至爲了運行這幾行而構建了示例項目,它們都以相同的方式運行。請請幫助(最終目標是使以https這項工作很好,但任何進展,使工作與HTTP是我不夠好)HttpResponse不會工作
try{
HttpClient myHttpClient = new DefaultHttpClient();
HttpPost myHttpPost = new HttpPost("http://www.siirretytnumerot.fi/index.html?clientLanguage=eng");
//^Ive tried about 10 different sites (http and https) none of them work
//NEVER GETS PAST THE LINE BELOW, JUMPS TO THE LAST CATCH BLOCK OF THE EXCEPTION
HttpResponse response = myHttpClient.execute(myHttpPost);
//NEVER GETS PAST THE ABOVE LINE...
String data = EntityUtils.toString(response.getEntity());
json= new JSONObject(data);
//parse the JSONObject
} catch (UnsupportedEncodingException e){e.printStackTrace();}
catch (ClientProtocolException e){e.printStackTrace();}
catch (IOException e){e.printStackTrace();}
catch (JSONException e) {e.printStackTrace();}
catch (NullPointerException e){ Log.e("My APP", "exception: " + e.getMessage());}
catch (Exception e) {Log.e("My APP", "exception", e);}
10-17 15:31:41.423: E/My APP(974): exception
10-17 15:31:41.423: E/My APP(974): android.os.NetworkOnMainThreadException
10-17 15:31:41.423: E/My APP(974): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-17 15:31:41.423: E/My APP(974): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-17 15:31:41.423: E/My APP(974): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-17 15:31:41.423: E/My APP(974): at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-17 15:31:41.423: E/My APP(974): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-17 15:31:41.423: E/My APP(974): at com.example.httpstuff.MainActivity$1.onClick(MainActivity.java:44)
10-17 15:31:41.423: E/My APP(974): at android.view.View.performClick(View.java:3511)
10-17 15:31:41.423: E/My APP(974): at android.view.View$PerformClick.run(View.java:14105)
10-17 15:31:41.423: E/My APP(974): at android.os.Handler.handleCallback(Handler.java:605)
10-17 15:31:41.423: E/My APP(974): at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 15:31:41.423: E/My APP(974): at android.os.Looper.loop(Looper.java:137)
10-17 15:31:41.423: E/My APP(974): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 15:31:41.423: E/My APP(974): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 15:31:41.423: E/My APP(974): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 15:31:41.423: E/My APP(974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 15:31:41.423: E/My APP(974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 15:31:41.423: E/My APP(974): at dalvik.system.NativeStart.main(Native Method)
當在最後一個catch語句中捕獲到異常時,它是什麼類型的異常? – boztalay
我的catch和log語句設置是否正確(仍然試圖更好地理解log cat)?以上是我的相關(在我眼中)logcat結果 –