2013-05-07 51 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。