2014-01-14 26 views
0

以下是我的Fragment中的AsyncTask類,當我單擊片段時,出現此錯誤: NetworkOnMainThread異常(由於我的信譽不夠高,我無法發佈圖像)即使使用AsyncTask也會拋出NetworkOnMainThread異常

private class me extends AsyncTask<Object,Void,Boolean>{ 
      DisplayMetrics dm = new DisplayMetrics(); 

      protected void onPreExecute(){ 
       String sess = SignIn.giveSession().getSession().getToken(); 


       deviceId = device.getDeviceId(); 
       data = RestQuery.profileImage(sessionToken, deviceId, username); 
       bm = BitmapFactory.decodeByteArray(data,0,data.length); 
       getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); 


       username = reWiPr.getName(); 



      } 

      protected Boolean doInBackground(Object... stuff){ 
       if(reWiPr == null){ 
        return false; 
       } 

       return true; 
      } 

      protected void onPostExecute(Boolean results){ 
       if(results){ 
        image.setMinimumHeight(dm.heightPixels); 
        image.setMinimumWidth(dm.widthPixels); 
        image.setImageBitmap(bm); 

        uName.setText(reWiPr.getName()); 
        rName.setText(reWiPr.getRealName()); 
        safety.setText("Safety: " + reWiPr.getRating().getSafe()); 
        time.setText("Time: " + reWiPr.getRating().getTime()); 
        courteous.setText("Courteous: " + reWiPr.getRating().getCourteous()); 
        cleanliness.setText("Cleanliness: " + reWiPr.getRating().getClean()); 
        overall.setText("Overall: " + reWiPr.getRating().getOverall()); 
        average.setText("Average: " + reWiPr.getRating().getAverage()); 
        ve.setText("Vehicles: " + reWiPr.getVehicle()); 


       } 
      } 

我該如何解決這個問題?我需要這個AsyncTask來使這個片段工作,並且我在onCreate()方法中執行它。任何幫助,將不勝感激謝謝。*

回答

3

你沒有以正確的方法進行網絡調用。你必須移動你的網絡電話doInBackground,我現在看到它在onPreExecute

從文檔:

When an asynchronous task is executed, the task goes through 4 steps:

  1. onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
+0

我的網絡電話是一個RestQuery,我將其移動到doInBackground,但我仍然得到相同的錯誤。 – shreyashirday

+0

@ user3140562:你可以發佈你的新代碼嗎? – Nerd

+0

事實證明,我正在使用另一個類,它使用doInBackground之外的數據調用方法。無論如何,在AsyncTask的doInBackground中創建一個方法,然後在另一個單獨的類(不是父類)中調用該方法? – shreyashirday

0

你需要做你的網絡任務中doInBackground方法。目前您已在onPreExecute中運行GUI線程。

相關問題