2015-03-02 31 views
-1

我正在開發一個使用MySQL數據庫的登錄應用程序。我正在使用數據庫包中的JSONParser連接到本地mysql數據庫我收到以下錯誤,請有人幫助我。我搜索了這個錯誤,但我得到的是使用AsyncTask,但我不知道在哪裏使用,如何使用以及Mainthread是什麼。 請有人編輯我的代碼,並解釋或交相關代碼... 「android.os.NetworkonMainThreadException」當我從運行4.2 genymotion模擬器應用程序的錯誤...如何在JSON解析器中爲我的代碼編寫AsyncTask?

package com.android.database; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
      Log.e("JSON", json); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json);    
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
}`` 
+0

您嘗試調用UI線程網絡請求你應該這樣做在新的線程或的AsyncTask的[android.os.NetworkOnMainThreadException] – 2015-03-02 12:24:26

+0

可能重複(http://stackoverflow.com/questions/6343166 /機器人-OS-networkonmainthreadexception) – 2015-03-02 12:29:58

回答

0

網絡運營必須在完成在背景

您可以使用AsyncTask來完成此操作。

0

您正在UI線程上執行網絡操作,您無法執行此操作。考慮使用AsyncTask來完成這種操作,或者使用外部庫如this,它允許您以非常簡單的方式異步製作Http請求。

0

我不知道如果我正確地理解你的問題,我想,而添加評論,但我不允許(太低REP)所以這裏有雲:

該類JSONParser我假設是你正在談論的「活動」。這不,雖然一個活動,而是一類,所以你可以而創建這個類的一個新對象,並用它從你的呼叫活動:

JSONParser json = new JSONParser(); 
json.getJSONFromUrl(url,params); 

事情是,這樣做解析需要時間,主UI線程這可能會暫停線程,甚至可能讓應用程序崩潰,如果線程需要超過5秒的響應時間。這意味着你應該使用AsyncTask來運行這個JSONParser方法。開始學習AsyncTask的好地方是Vogella。他在他的網站上有一些很棒的教程。

您可能會使用doInBackground來運行getJSONFromUrl方法,然後從onPostExecute方法更新您的UI線程。所以基本上:使用的AsyncTask