2012-12-11 21 views
0

大家好我想傳遞一個變量的AsyncTask如何傳遞一個參數要的AsyncTask(Ksoap2)

我有這個變量

private static String NAMESPACE = "aaa"; 
private static String METHOD_NAME = "bbb"; 
private static String SOAP_ACTION = NAMESPACE + METHOD_NAME ; 
private static String URL = "ccc"; 

和我有這個任務

public class Login extends AsyncTask<Void, Void, String> 
    { 
    ProgressDialog progress; 
String response = ""; 
    private ProgressDialog pDialog; 
public void onPreExecute() 
    { 
    super.onPreExecute(); 
    pDialog = new ProgressDialog(MainActivity.this); 
    pDialog.setMessage("Please Wait"); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(false); 
    pDialog.show(); 
    } 
    @Override 
protected String doInBackground(Void... arg0)  { 
     final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);    
     request.addProperty("username", user_name); 
     request.addProperty("userpass", user_pass); 
     final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.setOutputSoapObject(request); 
     envelope.dotNet = true; 
     try 
      { 
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
        androidHttpTransport.call(SOAP_ACTION, envelope);      
        SoapPrimitive result = (SoapPrimitive) envelope.getResponse();   
        response = result.toString(); 
      } 
     catch (IOException e) 
      { 
      response = "Error In The Operation(1) !!\n Check Internet Connection And TRY AGAIN."; 
      } 
     catch (Exception e) 
      { 
      response = "Error In The Operation(2) !!\n Check Internet Connection And TRY AGAIN."; 
      } 
    return response; 
    } 
@Override 
public void onPostExecute(String res) 
{ 
      if(!(res.equalsIgnoreCase(""))) 
      { 
        if (res.toString().contains(",") == true) 
        { 
        String[] separated = res.split(","); 
        tv.setText(separated[1]); 
        return; 
        } 

       if(res.toString().equals("1")) 
       { 
        res = "Wrong User name OR password ,, TRY AGAIN .."; 
        tv.setText(res); 
        pDialog.dismiss(); 
        return; 
       } 
       if(res.toString().equals("2")) 
       { 
        res = "Your Account Is temporarily Blocked ,, Please Call The Admin"; 
        tv.setText(res); 
        pDialog.dismiss(); 
        return; 
       } 
       if(res.toString().equals("3")) 
       { 
        res = "Error While Retrieve S Information ,, Try Again Later ."; 
        tv.setText(res); 
        pDialog.dismiss(); 
        return; 
       } 
       tv.setText(res); 
       pDialog.dismiss(); 
      } 
} 
    } 

我需要當我要執行這個Taks

稱它並通過上述變量

new Login().execute(); 

做它

new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS); 

隨着Knolledge這個任務返回一個字符串:)

doInBackground必須有USER_NAME值& user_pass需要通過它與執行電話..

Regards ...

+0

看來你已經有了解決方案爲您的問題... –

+0

我認爲問題就在這裏 **公共類登錄延伸AsyncTask ** – Loai

+0

我應該傳遞5值(METHODNAME,NAMESPACE,SOAPACTION,USER_NAME,USER_PASS)此任務... 而且它只有3個可用值要通過 – Loai

回答

3

爲什麼不創建登錄類的構造函數,如下面?在我的例子中,我將一個Activity傳遞給AsyncTask,以便在完成時調用回調函數,但在您的情況下,您還可以傳遞一個字符串數組。

在這種情況下,args數組傳遞給類構造函數,而params數組傳遞給doInBackground函數。 MainActivity傳遞給AsyncTask,以便在任務完成後可以在MainActivity中調用taskDone回調。

public class Login extends AsyncTask<String, Void, String> 
{ 
    private MainActivity activity; 

    //These private strings are only needed if you require them 
    //outside of the doInBackground function.... 
    //If not, just use the params argument of doInBackground by itself 
    private String METHODNAME, 
    private String NAMESPACE; 
    private String SOAPACTION; 
    private String USER_NAME; 
    private String USER_PASS; 

    public Login(String[] args, MainActivity activity) { 
     this.NAMESPACE= args[0]; 
     this.METHODNAME = args[1]; 
     this.SOAPACTION = args[2]; 
     this.USER_NAME = args[3]; 
     this.USER_PASS= args[4]; 

     this.activity = activity; 
    } 
    @Override 
    protected Void doInBackground(String... params) { 
     //Again, use either params local to this function 
     //or args local to the entire function... 
     //both would be redundant 
     String _NAMESPACE = params[0]; 
     String _METHODNAME = params[1]; 
     String _SOAPACTION = params[2]; 
     String _USER_NAME = params[3]; 
     String _USER_PASS= params[4]; 

     //Do background stuff 
    } 

    protected void onPostExecute() { 
     //dismiss progress dialog if needed 
     //Callback function in MainActivity to indicate task is done 
     activity.taskDone("some string"); 
    } 
} 

MainActivity.java

private String[] args= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; //to pass to constructor 
private String[] params= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; //to pass to doInBackground 

//Pass your args array and the current activity to the AsyncTask 
new Login(args, MainActivity.this).execute(params); 

//Callback for AsyncTask to call when its completed 
public void taskDone(String returnVal) { 
    //Do stuff once data has been loaded 
    returnText = returnVal; 
} 
+0

而且,正如其他人所建議的,您需要將您的類定義更改爲'公共類登錄擴展AsyncTask ' – jamis0n

+0

+1它工作正常,謝謝你:) – Loai

2

讓該課程的登錄延長AsyncTask<String, Void,String>並將doInBackground(Void... params)更改爲doInBackground(String... params)。 現在您可以通過想要的方式執行任務new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);,並通過params []數組訪問給定的參數。 這意味着你的例子:params [0] == URL,params [1] == NAMESPACE等等。

1

的第一件事是你需要改變

public class Login extends AsyncTask<Void, Void, String>

public class Login extends AsyncTask<String, Void, String>

,改變

doInBackground(Void...

doInBackground(String...

Here is some very helpful documentation on it。如果您仍然遇到問題,請更具體地瞭解正在或未正在處理的問題。

由異步任務中使用的三種類型的有以下幾種:

PARAMS,在執行時發送給任務的參數的類型。

進度,在 背景計算期間發佈的進度單位的類型。

結果, 背景計算結果的類型。

這些是<String, Void, String>的參數。第一String是什麼是傳遞這就是爲什麼在doInBackground()你有String...,指示和字符串數組傳遞

1

,只要你想你可以把儘可能多的PARAMS中執行,因爲doInbackground(參數... PARAMS)(認爲它作爲PARAMS [] PARAMS)接受盡可能多的只要它們是相同類型的參數。

但如果你的參數是不同類型的(這不是你的情況下),你需要讓他們爲的AsyncTask類的屬性,並通過您的AsyncTask構造函數new login(type1 attr1, type2 attr2).execute(params)通過他們的價值觀:

相關問題