2016-05-31 49 views
0

你能帮我吗,我的代码运行之前,我试图实施asynctask在我的应用程序中显示progressdialog,这是一个自从一周以来,我尝试了几个解决方案,但仍然是同样的问题,请有人纠正我代码, 我的代码是:progressdialog使用asynctask fecthing数据

package coma.exame.dbselect; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Color; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.TableRow.LayoutParams; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends Activity { 
    InputStream is = null; 
     String result = null; 
     JSONObject contact = null; 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
       Button button = (Button) findViewById(R.id.button1); 
       button.setOnClickListener(new View.OnClickListener() 
        { 
         public void onClick(View view) 
        { 
        new task().execute(); 

       } 
       }); 
      } 

    class task extends AsyncTask<String, String, String> 
      { 
      private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);//private ProgressDialog progressDialog = new ProgressDialog(Login.this); 
      protected String doInBackground(String... params) { 
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);   
       try{ 
         HttpClient httpclient = new DefaultHttpClient(); 
         HttpPost httppost = new HttpPost("http://192.168.1.11/sqlserv/test.php"); 
         HttpResponse response = httpclient.execute(httppost); 
         HttpEntity entity = response.getEntity(); 
         is = entity.getContent(); 
       } 
       catch(Exception e) 
       { 
         Log.e("log_bad_connection", "Error in http connection "+e.toString()); 
         Toast.makeText(getApplicationContext(), "Tsy tafa ny connexion", Toast.LENGTH_SHORT).show(); 
       } 
          try 
         { 
           BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
           StringBuilder sb = new StringBuilder(); 
           String line = null; 
           while ((line = reader.readLine()) != null) 
           { 
             sb.append(line + "\n"); 

           } 
           is.close(); 

           result=sb.toString(); 
           return result; 
         } 
         catch(Exception e) 
         { 
           Log.e("log_conversion_resultat", "Error converting result "+e.toString()); 
          Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show(); 

         } 


      } 

protected void onPreExecute() { 
    progressDialog.setMessage("Download data..."); 
    progressDialog.show(); 

    } 

       protected void onPostExecute(String result) { 

        JSONArray jArray = new JSONArray(result); 

         TableLayout tv=(TableLayout) findViewById(R.id.table); 
         tv.removeAllViewsInLayout(); 

        for(int i=-1;i<jArray.length();i++) 

           { 
             TableRow tr = new TableRow(MainActivity.this); 

             tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

             try { 

              JSONObject json_data = jArray.getJSONObject(i); 

            TextView b=new TextView(MainActivity.this); 
             String stime=String.valueOf(json_data.getString("AR_REF")); 
             b.setText(stime); 
             b.setTextColor(Color.BLACK); 
             b.setTextSize(6); 
             tr.addView(b); 
             tv.addView(tr); 
           final View vline1 = new View(MainActivity.this); 
          vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
          vline1.setBackgroundColor(Color.RED); 
          tv.addView(vline1);  

             } 

          } 
        this.progressDialog.dismiss(); 
        } 

         catch(JSONException e) 
         { 
           Log.e("log_tag", "Error parsing data "+e.toString()); 
           Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show(); 
         } 

} 

} 
} 

我logcat的表示错误:

12月5日至31日:13:09.437:I /指导(698):跳过64帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:10.159:I /编舞师(698):跳过60帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:10.619:D/gralloc_goldfish(698):没有GPU仿真的仿真器 检测到。 05-31 12:13:25.900:我/编舞(698):跳过111 帧!该应用程序可能在其主要的 线程上做了太多工作。 05-31 12:13:26.334:I /编舞师(698):跳过35帧! 该应用程序可能在其主线程上做了太多工作。 05-31 12:13:26.808:I/Choreographer(698):跳过了50帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.214:I /编舞师(698):跳过106帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.413:I /编舞师(698):跳过41帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.613:I /编舞师(698):跳过32帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.844:I /编舞师(698):跳过了58帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.113:I /编舞师(698):跳过59帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.364:I /编舞师(698):跳过了57帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.634:I /编舞师(698):跳过55帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.894:I /编舞师(698):跳过62帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:29.178:I/Choreographer(698):跳过64帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:29.484:I /编舞师(698):跳过75帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:29.754:I /编舞师(698):跳过了57帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.034:我/编舞师(698):跳过35帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.294:I /编舞师(698):跳过53帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.573:I /编舞师(698):跳过66帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.804:I /编舞师(698):跳过53帧! 应用程序可能在其主线程上做了太多工作。 05-31 ...

谢谢

+0

你能告诉我如何使用它吗?因为我已经在我的代码中使用PreExecute方法 – Ratoj

+0

好吧。我们知道您的应用程序在主线程中做了太多工作。 –

+0

'ProgressDialog'是否显示? –

回答

0

可以按照这样的:

public class ReadWebpageAsyncTask extends Activity { 
private TextView textView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
textView = (TextView) findViewById(R.id.TextView01); 
} 

private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
@Override 
protected String doInBackground(String... urls) { 
    String response = ""; 
    for (String url : urls) { 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(url); 
    try { 
     HttpResponse execute = client.execute(httpGet); 
     InputStream content = execute.getEntity().getContent(); 

     BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
     String s = ""; 
     while ((s = buffer.readLine()) != null) { 
     response += s; 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
    return response; 
} 

@Override 
protected void onPostExecute(String result) { 
    textView.setText(result); 
} 
} 

public void onClick(View view) { 
    DownloadWebPageTask task = new DownloadWebPageTask(); 
    task.execute(new String[] { "http://www.vogella.com" }); 

    } 
} 
+0

总是同样的问题:我/编舞(698):跳过了48帧!该应用程序可能在其主线程上做了太多工作.......... – Ratoj

+0

我可以删除未使用的参数吗? – Ratoj

+0

感谢您的回复,我尝试了 – Ratoj

0

首先,这不是一个错误。这是Logcat中的INFO,它警告您在UI线程中执行的一些密集任务。正如我所看到的,您已经使用AsyncTask来完成CPU密集型任务。您正在使用仿真器,并且在仿真器上跳过帧将成为问题。如果它跳过太多,则需要查看在UI线程上执行的CPU密集型任务。在实际设备上测试它,它可能不会显示该警告。

+0

感谢您的解释Abdul Fatir – Ratoj