【Android AsyncTask问题(连接到Web服务)】不飞则已,一飞冲天;不鸣则已,一鸣惊人。这篇文章主要讲述Android AsyncTask问题(连接到Web服务)相关的知识,希望能为你提供帮助。
所以我正在开发应用程序的登录活动,我想使用AsynTask连接到(SOAP)Web服务,我将向Web服务发送一个密钥,用户名和密码,这将给我一个布尔结果,无论是用户是否有效....在尝试AsyncTask之前,我使用的是我在网上找到的方法,你创建一个-caller类 - 扩展-Thread类 - 并在-MainActivity中使用while循环 - 这使得-UI成为-UI线程睡眠 - 好吧它工作但它感觉有点不正确的方法,所以我调查它并发现AsyncTask但我似乎无法让它工作... PS:我不能分享网络服务的网址,因为它属于我的大学,对不起。
public class LoginActivity extends AppCompatActivity {Button loginButton;
EditText usernumber,password;
boolean result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
loginButton = (Button) findViewById(R.id.loginButton);
usernumber = (EditText) findViewById(R.id.userNum);
password = (EditText) findViewById(R.id.userPass);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new CallLoginWS().execute();
if(result == true){
Intent bEmpIntent = new Intent(LoginActivity.this,BasicEmployeeActivity.class);
startActivity(bEmpIntent);
}else{
Toast.makeText(LoginActivity.this,"username or password incorrect",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}}
});
}public class CallLoginWS extends AsyncTask<
Void,Void,Void>
{private final String SOAP_ACTION = "";
private final String OPERATION_NAME = "IsValidUser";
private final String WSDL_TARGET_NAMESPACE = "";
private final String HOST = "";
private final String FILE = "";
private final String KEY = "";
@Override
protected Void doInBackground(Void... voids) {
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("usagekey");
pi.setValue(KEY);
pi.setType(String.class);
request.addProperty(pi);
pi = new PropertyInfo();
pi.setName("username");
pi.setValue(usernumber.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
pi = new PropertyInfo();
pi.setName("password");
pi.setValue(password.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpsTransportSE httpsTransport = new HttpsTransportSE(HOST,443,FILE,15000);
SSLConnection ssl = new SSLConnection();
ssl.allowAllSSL();
try
{
httpsTransport.call(SOAP_ACTION, envelope);
result = (boolean)envelope.getResponse();
}
catch (Exception exception)
{
exception.printStackTrace();
}
return null;
}
}
}
我是新手,我现在真的输了,所以任何帮助/建议都非常感谢。
答案您需要移动此代码
if(result == true){
Intent bEmpIntent = new Intent(LoginActivity.this,BasicEmployeeActivity.class);
startActivity(bEmpIntent);
}else{
Toast.makeText(LoginActivity.this,"username or password incorrect",Toast.LENGTH_SHORT).show();
}
你的asynctask的onPostExecute()
推荐阅读
- AsyncTask Android方法的技术差异
- Android(Kotlin) - 如何等待异步任务完成()
- E / AndroidRuntime(致命异常:Android的AsyncTask#1 ERROR)
- Android AsyncTask无法运行
- AsyncTask在android中加载数据的次数非常不同
- Android Socket,AsyncTask,Handler内存泄漏
- 示例(使用AsyncTask的Android双向网络套接字)
- 如何在没有AppEngine for Google云端存储的情况下获取服务网址()
- 带有Gcloud错误的Rails((gcloud.preview.app.deploy)错误响应:[13]意外错误。部署版本:)