我是android的新手。我正在尝试通过ksoap在异步任务中使用.net webservise。但是应用程序崩溃。我不知道问题出在哪里。这是我的代码...请帮助我...... / p>
public class LoginActivity extends Activity {
private String METHOD_NAME = "AuthenticateUser"; // our webservice method name
private String NAMESPACE = "http://tempuri.org/";
private String SOAP_ACTION = "http://tempuri.org/AuthenticateUser";
// private String SOAP_ACTION = "http://LocationBasedTaxiServices/UserWebServices.asmx/RegisterUser"; // NAMESPACE + method name
private static final String URL = "http://192.168.0.45/LocationBasedTaxiServices/UserWebServices.asmx?WSDL"; // you
//////////////////////getting email amd password from R file////////////////
private EditText login_email;
private EditText login_password;
private String result;
private Button loginbtn;
private TextView registerTextView;
String user_id;
String password;
String authentication;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginbtn=(Button)findViewById(R.id.btnLogin);
login_email=(EditText)findViewById(R.id.l_e);
login_password=(EditText)findViewById(R.id.l_p);
loginbtn.setOnClickListener(new View.OnClickListener() {
// SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
public void onClick(View v) {
user_id=login_email.getText().toString();
password=login_password.getText().toString();
new asynLogin().execute();
}
});
registerTextView =(TextView)findViewById(R.id.link_to_register);
registerTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), Register.class);
startActivity(i);
}
});
}
private class asynLogin extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(LoginActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Logging in...");
this.dialog.show();
}
protected Void doInBackground(final Void... unused) {
// authentication = doLogin(user_id, password);
authentication = doLogin(user_id,password);
return null; // don't interact with the ui!
}
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if (authentication.equals(true)) {
Intent i=new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
} else {
Toast.makeText(getApplicationContext(), "User Name Does Not exist", Toast.LENGTH_LONG).show();
}
}
private String doLogin(String user_id, String password) {
SoapPrimitive resultstring = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("email", user_id);
request.addProperty("password", password);
SoapSerializationEnvelope soapenvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapenvelope.dotNet = true;
soapenvelope.setOutputSoapObject(request);
HttpTransportSE httptransport = new HttpTransportSE(URL);
httptransport.debug = true;
try {
httptransport.call(SOAP_ACTION, soapenvelope);
resultstring = (SoapPrimitive) soapenvelope.getResponse();
result=resultstring.toString();
login_email.setText(result);
Log.i("myLogin", resultstring.toString());
System.out.println(resultstring);
} catch (SocketException ex) {
login_email.setText(ex.getMessage());
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
} catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
// return resultstring+"";
return result;
}
}
}
请帮助我。提前致谢。面对从一个活动到另一个活动的问题,而webservice响应与我想要的完全相同......
答案 0 :(得分:1)
您需要将finish()
从doInBackground()
移至onPOstExecute()
并替换此IF Condition
,否则您的代码工作正常。
if (authentication.equalsIgnoreCase("true")) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
} else if (authentication.equalsIgnoreCase("false")) {
Toast.makeText(LoginActivity.this, "User Name Does Not exist", Toast.LENGTH_LONG).show();
}
答案 1 :(得分:0)
您正在调用onPostExecute,它需要位于UI线程中。因为您遗留了无效的旧活动。
我想建议: - 转到新活动 - 显示加载对话框 - 异步任务完成时:关闭对话框。
更新: 如果你想继续这项活动,我会稍微改变一下代码:
if (authentication.equals(true)) {
myActivity.onAuthentificationSuccess(true);
//Intent i=new Intent(getApplicationContext(), MainActivity.class);
//startActivity(i);
//finish();
} else {
//Toast.makeText(getApplicationContext(), "User Name Does Not exist", Toast.LENGTH_LONG).show();
myActivity.onAuthentificationSuccess(false);
}
确保您的活动将通过以下方式实现您的界面:
void myActivity.onAuthentificationSuccess(boolean authenticated);
方法。你可以在那个方法中调用芬兰语或者你需要的任何东西哦,以及作为Async任务构造函数的活动/接口的实例。
private MyInterfaceWithOnAuthentificationMethod callbackObject;
public asynLogin(MyInterfaceWithOnAuthentificationMethod obj){
callbackObject = obj;
}
-refactor你的类,并给出一个大写的名称和接口缩短很多,大声笑