2013-02-11 82 views
3

我正在製作一個解析來自互聯網網頁源代碼的JSON文本的android程序。它在android 2.2中工作,但我現在需要它在Android 3.0上,它需要在AsyncTask上。我有一個關於AsyncTask的背景,但我很困惑把它放在哪裏。在此先感謝大家:)使用AsyncTask解析JSON對象的setText

這是我在MainActivity類方法:

private void jsonStuffs() { 
    //JSON PARSER & HOME PAGE TEXTVIEWS 

     client = new DefaultHttpClient(); 

     GetMethodEx test = new GetMethodEx(); 
     String returned; 
     try { 
      returned = test.getInternetData(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try{ 
      String jsonStr = test.getInternetData(); //go to GetMethodEx 
       JSONObject obj = new JSONObject(jsonStr); 

//////////////////////find temperature in the JSON in the webpage 

       String temperature = obj.getString("temperature"); 
       TextView tvTemp = (TextView)findViewById(R.id.textView); 
       tvTemp.setText(temperature); 

     } 
     //catch (JSONException e) { 
      // e.printStackTrace(); 
      //} 
     catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

的GetMethodEx類是這個(這將找到的網頁的鏈接,然後將其轉換的源代碼,以文本格式):

public class GetMethodEx extends Activity { 
    public String getInternetData() throws Exception{ 

     BufferedReader in = null; 
     String data = null; 
     // 

     try{ 
      HttpClient client = new DefaultHttpClient(); 
      URI website = new URI("http://nhjkv.comuf.com/json_only.php"); 
      HttpGet request = new HttpGet(); 
      request.setURI(website); 
      HttpResponse response = client.execute(request); 
      in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(""); 
      String l = ""; 
      String nl = System.getProperty("line.separator"); 
      while ((l = in.readLine()) !=null){ 
       sb.append(l + nl); 
      } 
      in.close(); 
      data = sb.toString(); 
      return data; 
     }finally { 
      if (in !=null){ 
       try{ 
        in.close(); 
        return data; 
       } catch (Exception e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
+0

你得到'NetworkOnMainThreadException'正確嗎? – Geros 2013-02-11 08:08:46

+0

谷歌之前,你要它。 – njzk2 2013-02-11 08:54:33

回答

1

它是工作在Android 2.2的,但我需要它現在是在Android 3.0, 這就需要將上的AsyncTask。

=>是的,如果您在不執行內部線程(如AsyncTask)的情況下進行Web調用,它會在3.0中給出NetworkOnMainThreadException

我有一個關於AsyncTask的背景,但我很困惑在哪裏把 這個和那個。

=>簡單地包括內部的的AsyncTask的doInBackground()方法網頁調用邏輯,你的情況呼叫getInternetData()內doInBackground()。如果在doInBackground()中執行長時間運行的任務時,不能直接更新UI。是的,如果你想更新用戶界面,然後按照以下任一操作:

  1. 從onPostExecute()方法更新UI。裏面的doInBackround()
2

你可以做這樣的事情(這代碼只是爲了說明,將其更改爲需要)

class MyAsyncTask extends AsyncTask<String, Void, JSONObject> { 
    protected void onPreExecute() { 
     // You can set your activity to show busy indicator 
     //setProgressBarIndeterminateVisibility(true); 
    } 

    protected JSONObject doInBackground(String... args) { 
     return jsonStuffs(); 
    } 

    protected void onPostExecute(final JSONObject jsonObj) { 
     String temperature = jsonObj.getString("temperature"); 
     TextView tvTemp = (TextView)findViewById(R.id.textView); 
     tvTemp.setText(temperature); 
     // Stop busy indicator 
     //setProgressBarIndeterminateVisibility(false); 
    } 

要調用這個任務使用new MyAsyncTask().execute();(你可以

  • 或實施runOnUiThread()通過String參數在需要時執行) 您可以更改您的jsonStuffs()返回JSONObject

    例如

    private JSONObject jsonStuffs() { 
    
        // ... 
    
        String jsonStr = test.getInternetData(); //go to GetMethodEx 
        return new JSONObject(jsonStr); 
    
        // ... 
    }