2012-07-11 158 views
0

我正在使用KSOAP發送使用Web服務存儲在數據庫中的詳細信息。Web服務沒有問題,它工作正常。此代碼工作時,我沒有用戶AsyncTask類。我是新來的android和這是我第一次嘗試使用AsyncTask類,它不工作。我附加了日誌貓錯誤,doinbackround方法有些問題。我究竟做錯了什麼?請幫助執行doInBackround方法時發生錯誤

public class Registration extends Activity{ 
private static final String SOAP_ACTION = "http://tempuri.org/register"; 
private static final String OPERATION_NAME = "register"; 
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx"; 
Button sqlRegister, sqlView; 

EditText sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.registration); 
sqlFirstName = (EditText) findViewById(R.id.etFname); 
sqlLastName = (EditText) findViewById(R.id.etLname); 
sqlEmail = (EditText) findViewById(R.id.etEmail); 
sqlMobileNumber = (EditText) findViewById(R.id.etPhone); 
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc); 

sqlUsername = (EditText) findViewById(R.id.etUsername); 
sqlPassword = (EditText) findViewById(R.id.etPwd); 

sqlRegister = (Button) findViewById(R.id.bRegister); 
sqlRegister.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     switch (v.getId()){ 
     case R.id.bRegister: 
     new LongOperation().execute(""); 
     break; 
     } 
    } 
    }); 
} 

private class LongOperation extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String firstname = sqlFirstName.getText().toString(); 
     String lastname = sqlLastName.getText().toString(); 
     String emailadd = sqlEmail.getText().toString(); 
     String number = sqlMobileNumber.getText().toString(); 
     String loc = sqlCurrentLocation.getText().toString(); 
     String uname = sqlUsername.getText().toString(); 
     String pwd = sqlPassword.getText().toString(); 

     SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
     Request.addProperty("fname", String.valueOf(firstname)); 
     Request.addProperty("lname", String.valueOf(lastname)); 
     Request.addProperty("email", String.valueOf(emailadd)); 
     Request.addProperty("num", String.valueOf(number)); 
     Request.addProperty("loc", String.valueOf(loc)); 
     Request.addProperty("username", String.valueOf(uname)); 
     Request.addProperty("password", String.valueOf(pwd)); 
     Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 
     HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
     try 
     { 
      httpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
      int result = Integer.parseInt(response.getProperty(0).toString()); 
      if(result == '1') 
      { 
       return "Registered"; 
      } 
      else 
      { 
       return "Not Registered"; 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 

    }  

    @Override 
    protected void onPostExecute(String result) { 
     if(result=="Registered") 
     { 
     Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
     } 
     else if(result =="Not Registered") 
     { 
     Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 
    } 
} 

enter image description here

////編輯

public class Registration extends Activity{ 
private static final String SOAP_ACTION = "http://tempuri.org/register"; 
private static final String OPERATION_NAME = "register"; 
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx"; 
Button sqlRegister, sqlView; 

EditText sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.registration); 
sqlFirstName = (EditText) findViewById(R.id.etFname); 
sqlLastName = (EditText) findViewById(R.id.etLname); 
sqlEmail = (EditText) findViewById(R.id.etEmail); 
sqlMobileNumber = (EditText) findViewById(R.id.etPhone); 
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc); 

sqlUsername = (EditText) findViewById(R.id.etUsername); 
sqlPassword = (EditText) findViewById(R.id.etPwd); 

sqlRegister = (Button) findViewById(R.id.bRegister); 
sqlRegister.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     switch (v.getId()){ 
     case R.id.bRegister: 
     new LongOperation().execute(""); 
     break; 
     } 
    } 
    }); 
} 

private class LongOperation extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String firstname = sqlFirstName.getText().toString(); 
     String lastname = sqlLastName.getText().toString(); 
     String emailadd = sqlEmail.getText().toString(); 
     String number = sqlMobileNumber.getText().toString(); 
     String loc = sqlCurrentLocation.getText().toString(); 
     String uname = sqlUsername.getText().toString(); 
     String pwd = sqlPassword.getText().toString(); 

     SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
     Request.addProperty("fname", String.valueOf(firstname)); 
     Request.addProperty("lname", String.valueOf(lastname)); 
     Request.addProperty("email", String.valueOf(emailadd)); 
     Request.addProperty("num", String.valueOf(number)); 
     Request.addProperty("loc", String.valueOf(loc)); 
     Request.addProperty("username", String.valueOf(uname)); 
     Request.addProperty("password", String.valueOf(pwd)); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 
     HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
     Log.d("work","work"); 
     try 
     { 
      httpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
      int result = Integer.parseInt(response.getProperty(0).toString()); 
      if(result == 1) 
      { 
       Log.d("reg","reg"); 
       return "Registered"; 
      } 
      else 
      { 
       Log.d("no","no"); 
       return "Not Registered"; 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 

    }  

    @Override 
    protected void onPostExecute(String result) { 
     Log.d("tag","onpost"); 
     if(result!=null) 
     { 

      if(result.equals("Registered")) 
       { 
        Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
       } 
      else if(result.equals("Not Registered")) 
       { 
        Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show(); 
       } 
     } 
     else 
     { 
      Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 

    } 

}

+0

其仍然沒有工作,對不對? – 2012-07-11 05:53:02

+0

你不工作:(只有日誌消息工作正在顯示日誌貓 – 2012-07-11 06:04:21

+0

嘗試記錄什麼字符串返回,並告訴我們 – 2012-07-11 06:07:24

回答

0

試試這個....

  1. 始終保持UI工作UI線程和非UI在非UI線程中工作。

  2. doInBackground是一個非UI線程,所以你不能在這個UI線程 上發佈任何東西。

  3. Toast語句是doInBackgroud中的問題,將其移至postExecute,即在UI線程上工作的 。

    Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
    
+0

謝謝我從doinbackround()刪除吐司現在我沒有得到任何錯誤,但問題是沒有發生。沒有顯示敬酒 – 2012-07-11 04:59:31

+0

您在doPost中宣佈的敬酒是否顯示? – 2012-07-11 05:09:04

+0

等待doInBackground中的任務完成,看到敬酒。保留Log語句以瞭解最新情況 – 2012-07-11 05:11:22

0

你打電話

Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();

內部doInBackground(),這不是在UI線程中運行。導致問題。

試試這個:

runOnUiThread(new Runnable() { 
    public void run() { 
     Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
    } 
}); 
} 

或移動Toast.makeText到在UI線程

+0

我從doInBackground()中刪除了Toast。我沒有收到任何錯誤,但是當我點擊註冊按鈕時沒有任何反應。沒有一個吐司會顯示:( – 2012-07-11 04:50:41

+0

如果你把它移走了,你怎麼能顯示吐司?而且你不需要移除它。試試我上面發佈的代碼。在runOnUiThread中運行'Toast.makeText'。它應該可以工作。 – user1417127 2012-07-11 05:34:11

0

運行。這是因爲您呼叫吐司在doInBackground()onPostExecute()方法。無論doInBackground中的UI如何,都會造成looper()異常。所以儘量讓在PostExecute(用戶界面)的AsynTask 作爲

@Override 
protected void onPostExecute(String result) { 

Toast.makeText(Registration.this, "You have been registered Successfully",Toast.LENGTH_LONG).show(); 

} 
} 

它可能對你有幫助..

0

你不應該使用==比較字符串。

protected void onPostExecute(String result) { 
     if(result=="Registered") 

這可能是意外行爲背後的原因。嘗試使用類似

result.equals("registered") 

閱讀this

+0

除了'doInBackground()'中的麪包外,當然還有 – 2012-07-11 05:07:59

+0

更改了兩個仍然沒有工作:/沒有烤麪包被顯示。檢查我的編輯 – 2012-07-11 05:36:31

+0

使用日誌或控制檯輸出來檢查你的'onPostExecute()'是否被調用。 – 2012-07-11 05:38:28

相關問題