Home > Mobile >  why textView can't execute here
why textView can't execute here

Time:02-16

in onPostExecute method textview at the end won't execute its stays in "hello world" not with json data i initialize it in the top tried to put it outside the for loop no doesn't help at all

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            pd.dismiss();
            StringBuilder stringBuilder = new StringBuilder();
            try {
                JSONArray array = new JSONArray(s);
                for(int i = 0 ; i<array.length(); i  ) {
                    JSONObject object = array.getJSONObject(i);
                    String user = object.getString("username");
                    String address = object.getString("address");
                    JSONObject object1 = new JSONObject(address);
                    String city = object1.getString("city");
                    String geo = object1.getString("geo");
                    JSONObject jsonObject = new JSONObject(geo);
                    String lat = jsonObject.getString("let");
                    stringBuilder.append("username is "   user   " city is "   city   " let is "   lat   "\n");
                    textView.setText(stringBuilder.toString());

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

CodePudding user response:

asyncTask class was deprecated in API level 30. https://developer.android.com/reference/android/os/AsyncTask

i recommend you use this solution:

Use the standard java.util.concurrent or Kotlin concurrency utilities instead.

ExecutorService exService  = Executors.newSingleThreadExecutor();
Handler handler=new Handler(Looper.getMainLooper);
exService.execute(new Runnnable(){
...
// your background works here 
//
//if you need change ui   use this:
handler.post(new Runnable(){
///change ui 
});

});

or if you want use AsyncTask use handler to run it on mainThread :

   new Hanndler(Looper.getMainLooper).post(new ...
    {
       textView.setText(stringBuilder.toString());
     });
  • Related