2013-05-07 48 views
0

此活動嘗試接收從另一活動傳遞的文本,並將其合併以形成帶查詢字符串的網址。然後將url傳遞給asynctask函數以從服務器獲取數據並將其顯示到textview。我無法理解哪裏出錯了。textView僅顯示標籤,而不是從服務器獲取的數據

public class GetDetails extends Activity { 
    public static final String address = null; 
    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.simpleview); 
     Bundle extras=getIntent().getExtras(); 
     String i= extras.getString("id"); 


     new DetailThread().execute(i); 
    } 

    class DetailThread extends AsyncTask<String, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 


     } 

     @Override 
     protected Void doInBackground(String... text) { 
      String ix=text.toString(); 
      String address = "http://server/foreach.php?id="+ix; 
      parseJson(address); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 


      };   
      } 
    void parseJson(String url) { 
     try { 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) 
        .show(); 
     } 

     // response to inputstream 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "UTF-8")); 

      sb = new StringBuilder(); 

      String line = null; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      is.close(); 

      result = sb.toString(); 
     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) 
        .show(); 
     } 
     // to string 
     try { 
      JSONArray jArray = new JSONArray(result); 
      JSONObject json_data = null; 
      String name=null; 
      String country=null; 
      TextView tv = (TextView) findViewById(R.id.textView1); 
      TextView tv2 = (TextView) findViewById(R.id.textView2); 


      for (int i = 0; i < jArray.length(); i++) { 
       json_data = jArray.getJSONObject(i); 
       name = "Name: "+json_data.getString("Name"); 
       country ="Country: "+json_data.getString("Country"); 
       tv.setText(name); 
       tv2.setText(country); 

      } 

     } 

     //json parsing exceptions handled here 
     } 


    } 

     } 

當我運行這是我剛剛得到的是標籤爲「大文本」和「中文字」,而不是數據必須被顯示在文本字段

+0

內'parseJson'方法,你都呈現吐司消息,也改變TextViews但目前調用'parseJson'方法形式' doInBackground'表示來自非UI線程。讓它工作返回數據或json來自'doInBackground'的字符串並使用'onPostExecute'在TextViews中顯示數據 – 2013-05-07 08:01:04

回答

1

您在doInBackground中調用parseJson方法。

doInBackground正在單獨的線程中運行。所以你不允許在非UI線程上執行UI操作。

你需要在textview使用

首先 runOnUiThread(Runnable接口)來更新值,即只需調用parseJson方法runonUIThread或doInBackground

第二返回JSON字符串並使用onPostExecute中的參數調用onPostExecute中的parseJSOn方法。

創建您的onCreate處理程序,並使用handler.post(runnbale)更新UI

0

您正試圖設置來自在獨立線程上運行的doInBackground()方法的TextView值。在Android中,您不允許從另一個線程更改UI。

您可以使用onPreExecute(),onPostExecute()或onProgressUpdate()與publishProgress()結合使用,它們都在UI線程上運行以更新TextView。