簡體   English   中英

textView僅顯示標簽,而不顯示從服務器獲取的數據

[英]textView displaying just labels instead from data fetched from server

此活動嘗試接收從另一個活動傳遞的文本,並將其組合以形成帶有查詢字符串的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
        }


    }

        }

運行此命令時,我得到的只是標簽為“大文本”和“中文本”,而不是必須在文本字段中顯示的數據

您已經在doInBackground調用了parseJson方法。

doInBackground在單獨的線程中運行。 因此不允許您在非UI線程上執行UI操作。

你需要使用

首先 runOnUiThread(Runnable接口),以在您的更新值textview ,即只需調用parseJson方法runonUIThread

第二個在doInBackground中返回json字符串,並在onPostExecute中使用參數調用onPostExecute中的parseJSOn方法。

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

您試圖通過在單獨線程上運行的doInBackground()方法設置TextView值。 在Android中,不允許您從其他線程更改UI。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM