2012-09-24 138 views
0

我知道這個問題已經被多次詢問過,但現在我很無奈。連接本地主機時出錯

我有一個PHP網頁在localhost回顯「你好」。 (在localhost上完美工作)。 我有下面的代碼顯示從我的應用程序本地主機網頁到TextView的響應。

tv = (TextView) findViewById(R.id.txtTest); 
InputStream is = null; 
String result = ""; 
    String url = "http://10.0.2.2/android/try.php"; 
    HttpClient httpclient = new DefaultHttpClient(); 

    try {    
     HttpPost httppost = new HttpPost(url); 

     HttpResponse response = httpclient.execute(httppost); 

     Log.d("myapp", "response " + response.getEntity()); 

     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
     String st = EntityUtils.toString(response.getEntity()); 


    } catch (Exception e) { 
     Log.e("log_tag", "Error in http connection " + e.toString()); 
    } 

    // convert response to string 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "UTF-8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error converting result " + e.toString()); 
    } 

    tv.setText(result.toString()); 

我得到以下錯誤(LogCat)。

09-24 13:34:42.654: E/log_tag(2032): Error in http connection android.os.NetworkOnMainThreadException 
09-24 13:34:42.654: E/log_tag(2032): Error converting result java.lang.NullPointerException 

P.S我已經在清單中添加Internet權限。

回答

3

您應該從主(UI)線程以外的線程執行網絡操作(連接等)。這就是你所得到的錯誤android.os.NetworkOnMainThreadException

你應該看看ASyncTask或Thread來做到這一點。

閱讀this article太...

這個替換您當前擁有的代碼:

AsyncTask<Void,Void,Void> my_task = new AsyncTask<Void,Void,Void>() { 
     @Override 
     protected void onPostExecute() { 
      TextView tv = (TextView) findViewById(R.id.txtTest); 
      tv.setText(result.toString()); 
     } 

     @Override 
     protected Void doInBackground(Void... voids) { 
      InputStream is = null; 
      String result = ""; 
      String url = "http://10.0.2.2/android/try.php"; 
      HttpClient httpclient = new DefaultHttpClient(); 

      try { 
       HttpPost httppost = new HttpPost(url); 

       HttpResponse response = httpclient.execute(httppost); 

       Log.d("myapp", "response " + response.getEntity()); 

       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
       String st = EntityUtils.toString(response.getEntity()); 


      } catch (Exception e) { 
       Log.e("log_tag", "Error in http connection " + e.toString()); 
      } 

      // convert response to string 
      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(
         is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error converting result " + e.toString()); 
      } 
     } 
    }.execute(); 
+0

謝謝馬修,但我一直無法使用的AsyncTask來運行它。你能幫我製作上面寫的代碼的AsyncTask類嗎? – Zeeshan

+0

謝謝,但現在我得到這個錯誤:( 09-24 15:30:10.095:E/log_tag(7027):錯誤轉換結果java.lang.RuntimeException:不能創建處理程序內部沒有調用Looper的線程.prepare() – Zeeshan

+0

什麼行是錯誤?你能發佈更多的代碼嗎?也許你應該考慮解決這個問題,並用你的新問題開始一個新的問題... – Matthieu