2014-10-10 45 views
-1

iam开发一个android应用程序,用于将json数据集解析到我的应用程序中。但每次的IAM得到一个NetworkOnMainThred例外:NetworkOnMainThread异常 - 以正确的方式解析数据

android.os.NetworkOnMainThreadException 

在此行中:

HttpResponse response = httpclient.execute(httppost); 

之后,香港专业教育学院试图puttin在内蒙古的AsyncTask类进度修复它。但是这并没有影响iam获得相同的错误。 AsyncTask真的很重要吗? 在这里,整个背景:

question.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Connector db = new Connector(); 
      db.executeAction();//calls AsyncTask 

     } 
    }); 




public class Connector extends Activity { 
    View rootView; 
    ArrayList<String> resultset = new ArrayList<String>(); 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


    } 
public void executeAction() { 
    new LongOperation().execute(); 
} 

private class LongOperation extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... params) { 
     testDB2(); 
     return null; 
    } 
    public void testDB2() { 

     String result = ""; 
     //the year data to send 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("year", "1980")); 

     //http post 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://quizmaster.esy.es/db_con.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 
      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(); 

      result = sb.toString(); 
     } catch (Exception e) { 
      //(TextView)rootView.findViewById(R.id.question) 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 
     ArrayList<String> resultset = new ArrayList<String>(); 
     //parse json data 
     try { 
      JSONArray jArray = new JSONArray(result); 

      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject json_data = jArray.getJSONObject(i); 
       resultset.add(String.format(json_data.getString("Frage"))); 
       Log.i("log_tag", "id: " + json_data.getInt("ID") + 
           ", Frage: " + json_data.getString("Frage") 
       ); 
      } 
     } 


     catch(JSONException e) 
     { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
    } 
    @Override 
    protected void onPostExecute(String result) { 

    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 
} 

}

调用:

public class Connector extends Activity { 
    View rootView; 
    ArrayList<String> resultset = new ArrayList<String>(); 

    /** Called when the activity is first created. */ 
    @Override public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public void executeAction() { 
     new LongOperation().doInBackground(); 
    } 
+1

'httpclient.execute()'步骤不在您发布的代码的主线程中。你如何执行AsyncTask? – Krylez 2014-10-10 19:18:35

+0

'code'public class Connector extends Activity {查看rootView; ArrayList resultset = new ArrayList (); /**当活动首次创建时调用。 */ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); } public void executeAction(){ new LongOperation()。doInBackground(); } – theAlgorithmGod 2014-10-10 19:19:55

+1

首先,不要手动调用'doInBackground()'方法,它会在调用'AsyncTask'上的execute()后的一段时间被框架调用。其次,请参阅@ 323go的答案。 – Krylez 2014-10-10 19:21:54

回答

2

onPostExecute()包含testDB2()通话。在主线程上执行onPostExecute()。因此例外。

此外,你从来没有直接致电doInBackground()。相反,您将调用AsyncTask为:

new LongOperation().execute(); 
+0

我现在尝试使用execute。但testDB2()现在不会被调用。 – theAlgorithmGod 2014-10-10 19:29:21

+0

并且错误包括进一步。 – theAlgorithmGod 2014-10-10 19:38:46

+0

你是否修复了“LongOperation”的调用? – 323go 2014-10-10 19:39:53