2012-07-11 21 views
2

我正在使用使用KSOAP的Web服務發送要存儲在數據庫中的詳細信息。我用Visual Studio創建了Web服務。該Web服務正常工作。細節被插入到數據庫中時將返回一個字符串。問題是這個字符串是空的,也許在我得到響應的方式上有些問題。我一直在努力弄清楚什麼是錯誤的。請幫助從Web服務收到的響應爲空

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:58076/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(); 
      String result = response.getProperty(0).toString(); 
      Log.d("res",result); 
      if(result.equals("reg")) 
      { 
       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(); ///This is what gets printed on screen 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

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

    } 
    } 

enter image description here

+0

你必須改變的價值SOAP_ACTION。 'SOAP_ACTION'是'http:// tempuri.org/register'。它應該是'http://10.0.2.2:58076/WebSite1/Service.asmx' – adatapost 2012-07-11 07:38:43

+0

嘗試仍然是一樣的。 – 2012-07-11 07:59:23

+0

首先使用SOAPUI工具來查看您的SOAP響應是否正如您所願.. – YuDroid 2012-07-11 08:23:16

回答

3

你的web服務返回String。 嘗試使用此方法解決你的問題

  Object result = envelope.getResponse(); 

時類型字節的web服務的返回值[],你可以這樣做:

  SoapObject response=(SoapObject)envelope.bodyIn; 

希望它可以幫助

+0

如果我可以投票你的答案100次我會......謝謝你! – 2012-07-11 09:16:03