0

此活动尝试接收从另一个活动传递的文本并将其组合成一个带有查询字符串的 url。然后将 url 传递给 asynctask 函数以从服务器获取数据并将其显示到 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
        }


    }

        }

当我运行它时,我得到的是标签为“大文本”和“中文本”,而不是必须在文本字段中显示的数据

4

2 回答 2

1

您已parseJsondoInBackground.

doInBackground在单独的线程中运行。所以你不能在非 UI 线程上执行 UI 操作。

你需要使用

首先runOnUiThread(Runnable) 更新你的值textview,即简单地调用parseJson方法runonUIThread

第二次 在 doInBackground 中返回 json 字符串,并使用 onPostExecute 中的参数调用 onPostExecute 中的 parseJSOn 方法。

第三次 在 onCreate 中创建处理程序并使用 handler.post(runnbale) 更新您的 UI

于 2013-05-07T08:02:24.847 回答
0

您正在尝试从在单独线程上运行的 doInBackground() 方法设置 TextView 值。在 Android 中,您不允许从另一个线程更改 UI。

您可以将 onPreExecute()、onPostExecute() 或 onProgressUpdate() 与 publishProgress() 结合使用,它们都在 UI 线程上运行以更新 TextView。

于 2013-05-07T08:02:01.950 回答