简体   繁体   中英

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. The url is then passed to asynctask function to obtain data from the server and display it to 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 is running in separate thread. so you are not allowed to perform UI operation on non UI thread.

you need to use

First runOnUiThread(Runnable) to update values in in your textview , ie simply call parseJson method in runonUIThread or

second return json string in doInBackground and using the paramter in onPostExecute call parseJSOn method in onPostExecute.

Third create handler in your onCreate and use handler.post(runnbale) to update your UI

You are trying to set the TextView values from the doInBackground() method which runs on a separate thread. In Android you are not allowed to change the UI from another thread.

You could use onPreExecute(), onPostExecute(), or onProgressUpdate() in conjuction with publishProgress() which all run on UI thread to update the TextViews.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM