使用AsyncTask解析JSON对象的setText

问题描述:

我正在制作一个解析来自互联网网页源代码的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

它是工作在Android 2.2的,但我需要它现在是在Android 3.0, 这就需要将上的AsyncTask。

=>是的,如果您在不执行内部线程(如AsyncTask)的情况下进行Web调用,它会在3.0中给出NetworkOnMainThreadException

我有一个关于AsyncTask的背景,但我很困惑在哪里把 这个和那个。

=>简单地包括内部的的AsyncTask的doInBackground()方法网页调用逻辑,你的情况呼叫getInternetData()内doInBackground()。如果在doInBackground()中执行长时间运行的任务时,不能直接更新UI。是的,如果你想更新用户界面,然后按照以下任一操作:

  1. 从onPostExecute()方法更新UI。里面的doInBackround()

你可以做这样的事情(这代码只是为了说明,将其更改为需要)

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); 
    
        // ... 
    }