2017-11-19 6 views
0

so bin ich neu in Android-Entwicklung, und ich habe gerade über ASyncTask gelernt. Ich benutze es in einer meiner App und immer noch bekomme ich die Warnung, dass: "Die Anwendung möglicherweise zu viel Arbeit auf seinem Haupt-Thread" und es überspringt auch Frames.Die Anwendung möglicherweise zu viel Arbeit auf seinem Haupt-Thread auch nach der Verwendung von ASyncTask

Ich Parsing einen JSON-Text von einer meiner Website, die Daten aus einer Datenbank zieht und konvertiert es in 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> 
+0

Mögliche Duplikat [Richtig Mit AsyncTask get()] (https://stackoverflow.com/questions/16007137/properly-using-asynctask-get) – user3707125

Antwort

1

Der Zweck des AsyncTask ist eine Arbeit außerhalb des Haupt-Thread zu tun, ohne es zu blockieren, und durch den Aufruf .get() auf dem AsyncTask:

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

Sie blockieren den Haupt-Thread, bis die AsyncTask ist fertig.

Versuchen Sie, das Ergebnis von der AsyncTask an die Benutzeroberfläche zu übergeben, indem Sie die Funktion onPostExecute (Result result) der AsyncTask überschreiben (die am Hauptthread funktioniert).

Beispiel von 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"); 
} 
} 
-1

Da es besagt, dass die Anwendung zu viel Arbeit auf seinem Hauptthread, das bedeutet, dass mehr Arbeit auf den Hauptthread gelegt wird.

Wenn die Frames, die übersprungen werden, mit in 100 sind, dann ist das kein großes Problem. Wie derzeit testen Sie die App mit dem Emulator. Wenn die Anzahl der Frames jedoch größer ist, müssen Sie mehr Threads in Ihrer App verwenden.

können Sie runOnUiThread hier, um Ihre Textview Wie dieses Update:

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); 
     } 
    })); 
} 

Ich hoffe, das hilft

Verwandte Themen