无法在android中检索Web方法的响应

时间:2012-07-12 16:14:15

标签: java android eclipse web-services ksoap2

我使用KSOAP调用Web服务。 Web方法工作正常,它返回字符串“无效”或“有效”。我试图传递从文本字段中获取的用户名和密码,并获得有效或无效的响应。我没有得到任何错误。我已经评论了代码,显示了在log cat中打印的内容以及没有打印的内容。我从Web方法获得响应的方式有问题。我究竟做错了什么?请帮忙

 public class Login extends Activity{

private static final String SOAP_ACTION = "http://tempuri.org/checkLogin";
private static final String OPERATION_NAME = "checkLogin";
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";
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
Button sqllogin;
EditText sqlusername, sqlpassword;
TextView tvData1;
CheckBox cb;

 @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sqlusername = (EditText) findViewById(R.id.etuname1);
    sqlpassword = (EditText) findViewById(R.id.etpass);
    tvData1 = (TextView)findViewById(R.id.textView1); 
    sqllogin = (Button) findViewById(R.id.bLogin);
    cb = (CheckBox) findViewById(R.id.cbrememberme);


    sqllogin.setOnClickListener(new View.OnClickListener() {

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

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

        @Override
        protected String doInBackground(String... params) {
            String username = sqlusername.getText().toString();
            String password = sqlpassword.getText().toString();

            SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
            Request.addProperty("uname", String.valueOf(username));
            Request.addProperty("pwd", String.valueOf(password));

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(Request);
            HttpTransportSE httpTransport  = new HttpTransportSE(SOAP_ADDRESS);
                    Log.d("work","work");//Only this Log message is displayed in log cat

                   try  {                    
                         httpTransport.call(SOAP_ACTION, envelope);                       
                         Object response = envelope.getResponse();
                         String result =  response.toString();
                         Log.d("res",result);//This is not displayed in log cat

                    if(result.equals("valid"))
                        {
                        Log.d("yes","yes");//This is not displayed in log cat
                        return "valid";
                        }
                    else if(result.equals("invalid"))
                        {
                        Log.d("no","no");//This is not displayed in log cat
                        return "invalid";
                        }
                    }
                   catch(Exception e){
                    e.printStackTrace();                           
                    }
                   return null;
        }

 @Override
 protected void onPostExecute(String result) {
    Log.d("tag","onpost");//This is not displayed in log cat
    if(result!=null)
    {
        if(result.equals("valid"))
            {

            tvData1.setText("You have logged in successfully");
            Intent openhomepage = new Intent("com.android.disasterAlertApp.HOME");
            startActivity(openhomepage);
            }
        else if(result.equals("invalid"))
            {
            tvData1.setText("Invalid Username or password");
            }
    }
    else
    {
        Toast.makeText(Login.this, "Somethings wrong", Toast.LENGTH_LONG).show();
}
 }

1 个答案:

答案 0 :(得分:2)

我弄清楚出了什么问题

而不是

httpTransport.call(SOAP_ACTION, envelope);                       
                     Object response = envelope.getResponse();
                     String result =  response.toString();

我应该使用

        httptransport.call(SOAP_ACTION, envelope);
        SoapPrimitive resultstring = (SoapPrimitive) soapenvelope
                        .getResponse();