2

我正在使用 KSOAP 调用 Web 服务。Web 方法运行良好,它将字符串返回为“无效”或“有效”。我正在尝试传入从文本字段中获取的用户名和密码,并在有效或无效时获得响应。我没有收到任何错误。我已经注释了代码,显示了在 log cat 中打印的内容和没有打印的内容。我从网络方法获取响应的方式有问题。我究竟做错了什么?请帮忙

 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();
}
 }
4

1 回答 1

2

我发现出了什么问题

代替

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

我应该使用

        httptransport.call(SOAP_ACTION, envelope);
        SoapPrimitive resultstring = (SoapPrimitive) soapenvelope
                        .getResponse();
于 2012-07-12T17:13:18.467 回答