2014-11-06 62 views
-3

只是想從我們的MySql服務器上的php文件中獲取一小部分信息來顯示連接是由應用程序進行的。在這一點上,它現在崩潰的應用程序和吐出org.Apache.http.client.clientprotocolexecption,原諒我,如果這是一個noob問題,我只有4個月編程在Java中。仍然得到android.os.NetworkOnMainThreadException,我做錯了什麼?

logcat的:

11-06 09:51:20.268: E/log.tag(2748): Error in http connection org.apache.http.client.ClientProtocolException 
    11-06 09:51:20.268: W/dalvikvm(2748): threadid=13: thread exiting with uncaught exception (group=0x40a13300) 
    11-06 09:51:20.288: E/AndroidRuntime(2748): FATAL EXCEPTION: Thread-83 
    11-06 09:51:20.288: E/AndroidRuntime(2748): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.view.View.invalidate(View.java:10250) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.view.View.invalidate(View.java:10205) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.widget.TextView.checkForRelayout(TextView.java:6296) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.widget.TextView.setText(TextView.java:3547) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.widget.TextView.setText(TextView.java:3405) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at android.widget.TextView.setText(TextView.java:3380) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at com.mobile.donswholesale.AppInfo$1$1.run(AppInfo.java:111) 
    11-06 09:51:20.288: E/AndroidRuntime(2748):  at java.lang.Thread.run(Thread.java:856) 

代碼:

test.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      new Thread(new Runnable() { 

       public void run() { 

        try { 

         HttpParams httpParams = new BasicHttpParams(); 
         HttpClient httpclient = new DefaultHttpClient(); 
         HttpPost httpost = new HttpPost("http://" 
           + serverIp.getText().toString() 
           + "/mobile.php"); 
         httpost.setHeader("Accept", "application/json"); 
         HttpResponse response = httpclient.execute(httpost); 
         HttpEntity entity = response.getEntity(); 
         isr = entity.getContent(); 
        } catch (Exception e) { 
         Log.e("log.tag", 
           "Error in http connection " + e.toString()); 
         resultView.setText("Could not connect to Database"); 
        } 
        try { 
         BufferedReader reader = new BufferedReader(
           new InputStreamReader(isr, "iso=8859-1"), 8); 
         StringBuilder sb = new StringBuilder(); 
         String line = null; 
         while ((line = reader.readLine()) != null) { 
          sb.append(line + "\n"); 
         } 
         isr.close(); 

         result = sb.toString(); 
        } catch (Exception e) { 
         Log.e("log.tag", 
           "Error converting result " + e.toString()); 
        } 

        try { 
         String s = ""; 
         JSONArray jArray = new JSONArray(result); 
         for (int i = 0; i < jArray.length(); i++) { 
          JSONObject json = jArray.getJSONObject(i); 
          s = s + "User :" + json.getString("UserName"); 
         } 
         resultView.setText(s); 
        } catch (Exception e) { 
         Log.e("log.tag", 
           "Error Parsing Data " + e.toString()); 
        } 

        Log.d(MainActivity.DEBUGTAG, "Didn't Work "); 
        return; 
       } 

       protected void onPostExecute(Void results) { 

       } 

      }).start(); 
     } 

    }); 
+0

您是否添加了權限: 2014-11-06 15:57:52

+1

什麼是logcat輸出? – 2014-11-06 15:58:22

+0

我懷疑你得到的例外,你認爲你是因爲線程看起來不錯,但它看起來你正嘗試從非UI線程更新一些你不能做的UI元素 – tyczj 2014-11-06 16:04:39

回答

3

你應該使用的AsyncTask的過程

 class A extends AsyncTask<Void, Void, String> { 
      protected String doInBackground(Void... params) { 
       String result = ""; 
       try { 

        HttpParams httpParams = new BasicHttpParams(); 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httpost = new HttpPost("http://" 
          + serverIp.getText().toString() 
          + "/mobile.php"); 
        httpost.setHeader("Accept", "application/json"); 
        HttpResponse response = httpclient.execute(httpost); 
        HttpEntity entity = response.getEntity(); 
        isr = entity.getContent(); 
       } catch (Exception e) { 
        Log.e("log.tag", 
          "Error in http connection " + e.toString()); 
        return null; 
        //resultView.setText("Could not connect to Database"); 
       } 
       try { 
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(isr, "iso=8859-1"), 8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        isr.close(); 

        result = sb.toString(); 
       } catch (Exception e) { 
        Log.e("log.tag", 
          "Error converting result " + e.toString()); 
       } 

       try { 
        String s = ""; 
        JSONArray jArray = new JSONArray(result); 
        for (int i = 0; i < jArray.length(); i++) { 
         JSONObject json = jArray.getJSONObject(i); 
         s = s + "User :" + json.getString("UserName"); 
        } 
        return s; 
       } catch (Exception e) { 
        Log.e("log.tag", 
          "Error Parsing Data " + e.toString()); 
       } 
      } 
      protected void onPostExecute(String s) { 
       if (s == null) { 
        resultView.setText("Could not connect to Database"); 
       } 
       else { 
        resultView.setText(s); 
       } 

} 

,然後只需

test.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     new A().execute(); 
    } 
}); 
0

你的問題是,你試圖在UI線程之外的視圖上設置文本。所以你已經爲http訪問創建了新的線程,現在你可以從同一個線程調用setText。如上所示,使用AsyncTask。