2013-04-08 66 views
0

我使用的AsyncTask類在我的第一個活動其做工精細 並調用第二個,我調用另一個的AsyncTask調用對象的onCreate 並調用Web服務在doInBackground 那麼它的給予例外 機器人.os.networkmainthread。如何使用的AsyncTask在Android 4.2

我沒有得到作爲 我是新來的Android開發 任何想法,所以請給我一些樣品或我告訴你我的代碼

這是我的代碼

public class panel extends Activity { 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.panel); 
    User_Balance_Load Balance_load=new User_Balance_Load(); 
    Balance_load.execute(); 
    Toast.makeText(getBaseContext(),Cls_Constant.username, Toast.LENGTH_LONG).show(); 



} 
class User_Balance_Load extends AsyncTask<Void, Void, Void> 
{ 
    private final ProgressDialog dialog = new ProgressDialog(panel.this); 
    protected void onPreExecute() { 

     this.dialog.setMessage("Loding Diet type..."); 
     this.dialog.show(); 
            } 

    protected Void doInBackground(final Void... unused) { 
     // TODO Auto-generated method stub 
     panel.this.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       Update_balance(); 
      } 
     }); 
     return null; 
    } 
    protected void onPostExecute(Void result) 
    { 

      if (this.dialog.isShowing()) 
      { 
      this.dialog.dismiss(); 
      } 


    } 

} 
void Update_balance() 
{ 

    Vector result; 
    result=Cls_webservice.User_Balance(Cls_Constant.Guid); 
    if(result.size()>0) 
    { 
     String UserBalance=result.elementAt(2).toString(); 
     TextView Tv_User_balacne=(TextView)findViewById(R.id.tv_point_balance); 
     Tv_User_balacne.setText(UserBalance); 
    } 
} 

這是我的類web服務的

public class Cls_webservice { 


public static Vector User_Balance(String id) 
    { 
     Vector _vector = new Vector(); 
     final String userid = id; 

     final String METHOD_NAME = "WCFDatatableRes"; 
     final String SOAP_ACTION = "http://tempuri.org/WCFDatatableRes"; 
     final String NAMESPACE = "http://tempuri.org/"; 
     final String URL = "http://xxxxxxxx.com/GameRoom/ANDROIDLOTT/WebService.asmx"; 
     String return_val=""; 
     SoapObject newob; 
     try 
     { 
      Object response; 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.encodingStyle = SoapEnvelope.ENC; 
      SoapObject Request = new SoapObject(NAMESPACE , METHOD_NAME); 
      Request.addProperty("Params", userid+",3"); 
      Request.addProperty("Do", "Balance"); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(Request); 
      AndroidHttpTransport httptransport ; 
      httptransport = new AndroidHttpTransport(URL); 

      httptransport.debug=true; 
      try 
      { 

       httptransport.call(SOAP_ACTION,envelope); 
       response = envelope.getResponse(); 
       newob = (SoapObject)envelope.bodyIn; 
       return_val = newob.toString(); 
       SoapObject diettype_listResult = (SoapObject) newob.getProperty("WCFDatatableRes ") ; 
       SoapObject diffgram = (SoapObject) diettype_listResult.getProperty("diffgram") ; 

      } 
      catch (Exception e) { 
       System.out.println("error:" + e); 
      }     

     } 
     catch (Exception e) { 
     } 
     return _vector; 
    } 

}

異常來自這一行 - >「httptransport.call(SOAP_ACTION,envelope); 所以請幫助我 和 我的第一個活動 相同的代碼的工作,我不知道爲什麼在第二次前來錯誤 感謝

,因爲你正在運行在UI線程網絡相關的操作時

回答

0

鏈接您正在使用的UI線程下的AsyncTask,然後在你的AsyncTask你正在運行的代碼。這是無稽之談,因爲結果與在OnCreate()方法上執行代碼相同。

protected Void doInBackground(final Void... unused) 
{ 
    Vector result = Update_balance(); 
    panel.this.runOnUiThread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      if(result.size()>0) 
      { 
       String UserBalance=result.elementAt(2).toString(); 
       TextView Tv_User_balacne=(TextView)findViewById(R.id.tv_point_balance); 
       Tv_User_balacne.setText(UserBalance); 
      }     
     } 
    }); 
    return null; 
} 

Vector Update_balance() 
{ 
    Vector result; 
    result=Cls_webservice.User_Balance(Cls_Constant.Guid); 
    return result; 
} 
+1

感謝JesusS :)謝謝,你極多 – Dharmeshsharma 2013-04-08 09:09:43

+0

別客氣 :) – JesusS 2013-04-08 09:46:46

1

NetworkOnMainThread例外。這隻適用於定位到Honeycomb SDK或更高版本的應用程序。針對早期SDK版本的應用程序允許在其主要事件循環線程上進行聯網,但非常不鼓勵。請參閱文檔Designing for Responsiveness。

您正在使用runonUithread運行Update_balance()。

您正試圖通過調用Update_balance()來更新doInBackground()中的ui,Update_balance()將textview中的文本設置爲如下所示。你不應該在doInBackground()中更新ui。

TextView Tv_User_balacne=(TextView)findViewById(R.id.tv_point_balance); 
    Tv_User_balacne.setText(UserBalance);TextView Tv_User_balacne=(TextView)findViewById(R.id.tv_point_balance); 
    Tv_User_balacne.setText(UserBalance); 

所有與網絡相關的操作都應該在doInBackground()中完成。在doInBackground()中進行web服務調用。

您可以返回值從doInBackground()onPostExecute()和更新UI相應檢索。

class TheTask extends AsyncTask<Void,Void,Void> 
{ 
protected void onPreExecute() 
{   super.onPreExecute(); 
     //display progressdialog. 
} 

protected void doInBackground(Void ...params)//return result here 
{ 
    //http request. do not update ui here 
    //call webservice 
    //return result here 
    return null; 
} 

protected void onPostExecute(Void result)//result of doInBackground is passed a parameter 
{  
     super.onPostExecute(result); 
     //dismiss progressdialog. 
     //update ui using the result returned form doInbackground() 

} 

} 

4個步驟 當執行一個異步任務,任務經過4個步驟:

  1. onPreExecute(),則執行該任務前的UI線程上調用。此步驟通常用於設置任務,例如通過在用戶界面中顯示進度條。

  2. onPreExecute()完成執行後立即在後臺線程上調用doInBackground(Params ...),此步驟用於執行可能需要很長時間的後臺計算。異步任務的參數傳遞給此步驟。計算結果必須通過該步驟返回並返回到最後一步。此步驟還可以使用publishProgress(Progress ...)發佈一個或多個進度單元。這些值在onProgressUpdate(Progress ...)步驟中發佈在UI線程上。

  3. onProgressUpdate(Progress ...),在調用publishProgress(Progress ...)後在UI線程上調用。執行的時間是未定義的。此方法用於在後臺計算仍在執行時在用戶界面中顯示任何形式的進度。例如,它可以用來爲進度條設置動畫效果或在文本字段中顯示日誌。

  4. onPostExecute(Result),在後臺計算完成後在UI線程上調用。後臺計算的結果作爲參數傳遞給此步驟。

欲瞭解更多詳情,請查看以下

http://developer.android.com/reference/android/os/AsyncTask.html

+0

感謝Raghuandan – Dharmeshsharma 2013-04-08 08:45:21

+0

如果我覺得有任何問題,我會通知你,所以請給我一些的Skype,Gmail或療法的ID,所以我可以聯繫你,當你自由感謝 – Dharmeshsharma 2013-04-08 08:45:39