0

so i'm new to android development and i just learned about ASyncTask. I'm using it in one of my app and still i am getting the warning that: "The application may be doing too much work on its main thread" and it is also skipping frames.

I'm parsing a json text from a my website which pulls data from a database and converts it into json.

TextView t1;
TextView t2;
TextView t3;
EditText editText;

public boolean checkNumber(String n){
    boolean flag=true;
    for(int i=0;i<n.length();i++){
        char ch=n.charAt(i);
        if(!(Character.isDigit(ch))){
            flag=false;
            break;
        }
    }
    return flag;
}

public static class getContent extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... urls) {
        String r="";
        URL url;
        HttpURLConnection httpURLConnection;

        try {

            url=new URL(urls[0]);
            httpURLConnection= (HttpURLConnection) url.openConnection();

            InputStream in=httpURLConnection.getInputStream();
            InputStreamReader reader=new InputStreamReader(in);

            int data=reader.read();

            while(data != -1){
                char ch=(char) data;
                r=r+ch;
                data=reader.read();
            }

            return r;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

public void findUser(View view) throws ExecutionException, InterruptedException, JSONException {
    t1=findViewById(R.id.nameView);
    t2=findViewById(R.id.emailView);
    t3=findViewById(R.id.countryView);
    editText=findViewById(R.id.numberEditText);

    String s=editText.getText().toString();

    if((s.length()==0) || (!checkNumber(s))){
        Toast.makeText(this, "Please enter a number...", Toast.LENGTH_SHORT).show();
    }else{
        getContent getContent=new getContent();
        String json=getContent.execute("http://www.website.com/Test/index.php?id="+s).get();

        JSONObject jsonObject=new JSONObject(json);
        String error=jsonObject.getString("error");
        if(error.equals("false")){
            String name=jsonObject.getString("name");
            String email=jsonObject.getString("email");
            String country=jsonObject.getString("country");

            t1.setText("Name: "+name);
            t2.setText("Email: "+email);
            t3.setText("Country: "+country);
        }else{
            Toast.makeText(this, "Cannot Find a user...", Toast.LENGTH_SHORT).show();
        }

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.vishurules.jsondata.MainActivity">

    <EditText
        android:id="@+id/numberEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp"
        android:ems="10"
        android:hint="@string/pick_a_number"
        android:inputType="number" />

    <Button
        android:id="@+id/goButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/numberEditText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:onClick="findUser"
        android:text="@string/go" />

    <TextView
        android:id="@+id/nameView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/goButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp"
        android:text="@string/name"
        android:textSize="32sp" />

    <TextView
        android:id="@+id/emailView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/nameView"
        android:layout_below="@+id/nameView"
        android:layout_marginTop="15dp"
        android:text="@string/email"
        android:textSize="32sp" />

    <TextView
        android:id="@+id/countryView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/emailView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="@string/country"
        android:textSize="32sp" />
</RelativeLayout>
  • Possible duplicate of [Properly Using AsyncTask get()](https://stackoverflow.com/questions/16007137/properly-using-asynctask-get) – user3707125 Nov 19 '17 at 07:57

2 Answers2

1

The purpose of the AsyncTask is to do some work outside the Main Thread without blocking it, and by calling .get() on the AsyncTask:

getContent.execute("http://www.website.com/Test/index.php?id="+s).get();

You are blocking the Main Thread until the AsyncTask is finished.

Try to pass your result from the AsyncTask to the UI by overriding the onPostExecute(Result result) function of the AsyncTask(Which works on the Main Thread).

Example from google:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
         // Escape early if cancel() is called
         if (isCancelled()) break;
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}
Yair P
  • 36
  • 6
-1

As it states that, the application is doing too much work on its main thread, that means more work is put on the main thread.

If the frames that is being skipped is with in 100 than that means its not a big problem. As currently you may be testing the app with the emulator. But if the number of frames are more than that, that means you need to use more threads in your app.

You can use runOnUiThread here to update your TextView Like this:

if(error.equals("false")){
     private void runThread(){
     runOnUiThread (new Thread(new Runnable() { 
         public void run() {
            String name=jsonObject.getString("name");
            String email=jsonObject.getString("email");
            String country=jsonObject.getString("country");

            t1.setText("Name: "+name);
            t2.setText("Email: "+email);
            t3.setText("Country: "+country);
         }
     }));
}

I hope that helps

Raddix
  • 113
  • 2
  • 9