2012-09-24 120 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