gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:35:15 26 4
gpt4 key购买 nike

我正在使用 KSOAP 调用 Web 服务。 Web 方法运行良好,它返回字符串“无效”或“有效”。我试图传入从文本字段中获取的用户名和密码,并在有效或无效时得到响应。我没有收到任何错误。我已经评论了显示日志猫中打印的内容和不打印的代码的代码。我从 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();
}
}

最佳答案

我知道哪里出了问题

代替

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

我应该用

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

关于java - 无法在 android 中检索 Web 方法的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11456135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com