2013-05-27 63 views
0

我使用soap方法發送請求。然後,我將字符串變量中的響應存儲在doInBackground中,然後檢查onPostExecute條件。 但正如我一步一步檢查它,我才知道這是清除值onPostExecute。 如何解決這個問題?AsyncTask在onPostExecute中清除響應

protected Void doInBackground(Void... args) { 

     request = new SoapObject(NAMESPACE, METHOD_NAME); 

     username = new PropertyInfo(); 
     username.setName("UserName"); 
     username.setValue(Username); 
     username.setType(String.class); 
     request.addProperty(username); 

     password = new PropertyInfo(); 
     password.setName("Password"); 
     password.setValue(Password); 
     password.setType(String.class); 
     request.addProperty(password); 

     SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     envp.dotNet = true; 
     envp.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     try { 
      androidHttpTransport.call(SOAP_ACTION, envp); 
      SoapPrimitive response = (SoapPrimitive) envp.getResponse(); 
      Response = response.toString(); 

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

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     if (dialog != null) { 
      dialog.dismiss(); 
      dialog = null; 
     } 

     if (Response.equals("Fail")) { 
      etPassword.setText(""); 
      textValidation.setText("UserName Or Password is Wrong."); 

     } else { 
      etUsername.setText(""); 
      etPassword.setText(""); 
     } 
    } 
+0

你的問題的一些代碼將是有益的。 –

+0

查看編輯過的帖子。 –

回答

0

你如何聲明你的AsyncTask類?

據異步任務使用的AsyncTask documentation

三種類型如下:

PARAMS,在執行時發送給任務的參數的類型。 進度,在 背景計算期間發佈的進度單位的類型。結果,背景計算結果的類型。並非所有類型都始終由異步任務使用。要將某個類型標記爲未使用,只需使用類型Void即可。

它應該是這樣的:

private class MyAsyncTask extends AsyncTask<Param, Progress, Result> 

這將定義doInBackground()簽名如下:

protected Result doInBackground(Param... params) 

然後,您可以在doInBackground()代替null最終返回結果。

編輯:

適用於您例如,應該看是這樣的:

public class MyAsyncTask extends AsyncTask<Void, int[], String> { 

    protected Void doInBackground(Void... args) { 

     request = new SoapObject(NAMESPACE, METHOD_NAME); 

     username = new PropertyInfo(); 
     username.setName("UserName"); 
     username.setValue(Username); 
     username.setType(String.class); 
     request.addProperty(username); 

     password = new PropertyInfo(); 
     password.setName("Password"); 
     password.setValue(Password); 
     password.setType(String.class); 
     request.addProperty(password); 

     SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
     SoapEnvelope.VER11); 
     envp.dotNet = true; 
     envp.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     try { 
      androidHttpTransport.call(SOAP_ACTION, envp); 
      SoapPrimitive response = (SoapPrimitive) envp.getResponse(); 
      // return your result here 
      return response.toString(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     // return null if something went wrong 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 

     if (dialog != null) { 
      dialog.dismiss(); 
      dialog = null; 
     } 

     if (result == null) { 
      // handle null here, for example : 
      // result = "Fail"; 
     } 

     // use "Fail".equals(result) instead of result.equals("Fail") 
     // to avoid error if result is null 
     if ("Fail".equals(result)) { 
      etPassword.setText(""); 
      textValidation.setText("UserName Or Password is Wrong."); 

     } else { 
     etUsername.setText(""); 
     etPassword.setText(""); 
     } 
    } 
}