简体   繁体   English

textView仅显示标签,而不显示从服务器获取的数据

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

this activity tries to receive the text passed from another activity and combines it to form a url with query string. 此活动尝试接收从另一个活动传递的文本,并将其组合以形成带有查询字符串的url。 The url is then passed to asynctask function to obtain data from the server and display it to textview. 然后将该URL传递给asynctask函数,以从服务器获取数据并将其显示在textview中。 I cant understand where ive gone wrong. 我不明白我在哪里出错了。

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
        }


    }

        }

When I run this what i just get are the labels as "Large Text" and "Medium Text" instead of data that has to be displayed in the text field 运行此命令时,我得到的只是标签为“大文本”和“中文本”,而不是必须在文本字段中显示的数据

You have called parseJson method in doInBackground . 您已经在doInBackground调用了parseJson方法。

doInBackground is running in separate thread. doInBackground在单独的线程中运行。 so you are not allowed to perform UI operation on non UI thread. 因此不允许您在非UI线程上执行UI操作。

you need to use 你需要使用

First runOnUiThread(Runnable) to update values in in your textview , ie simply call parseJson method in runonUIThread or 首先 runOnUiThread(Runnable接口),以在您的更新值textview ,即只需调用parseJson方法runonUIThread

second return json string in doInBackground and using the paramter in onPostExecute call parseJSOn method in onPostExecute. 第二个在doInBackground中返回json字符串,并在onPostExecute中使用参数调用onPostExecute中的parseJSOn方法。

Third create handler in your onCreate and use handler.post(runnbale) to update your UI 您的onCreate中的第三个创建处理程序,并使用handler.post(runnbale)更新您的UI

You are trying to set the TextView values from the doInBackground() method which runs on a separate thread. 您试图通过在单独线程上运行的doInBackground()方法设置TextView值。 In Android you are not allowed to change the UI from another thread. 在Android中,不允许您从其他线程更改UI。

You could use onPreExecute(), onPostExecute(), or onProgressUpdate() in conjuction with publishProgress() which all run on UI thread to update the TextViews. 您可以将onPreExecute(),onPostExecute()或onProgressUpdate()与publishProgress()结合使用,它们都在UI线程上运行以更新TextView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器获取数据时,如何从自动完成文本视图中获取所选项目的两个数据(id,name) - How to get both data (id ,name ) of selected item from autocomplete textview when data is fetched from server 将调用意图后从数据库中获取的数据设置为textview - set fetched data from database to a textview after calling intent Android在数据库中的TextView中显示数据 - Android displaying data in TextView from Database 将MYSQL数据库中的数据显示到android中的textview中 - displaying data from MYSQL database into textview in android 从列表视图检索数据并将其显示在textview / imageview中 - Retrieving data from a listview and displaying it in a textview/imageview 从文本文件读取数据并将其显示在textview上 - reading data from a textfile and displaying it on the textview 来自MySQL数据库的数据未显示在Android Textview中 - Data from a MySQL database is not displaying in Android Textview 从FirebaseDatabase获取的数据将显示在3个单独的AlertDialog中,而不是一个 - Data fetched from FirebaseDatabase is getting shown in 3 separate AlertDialogs instead of one 从服务器获取数据并在获取数据时刷新 UI? - Fetch data from server and refresh UI when data is fetched? 来自服务器的数据未显示在屏幕上 - Data from server is not displaying on screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM