Android 在asynchorinos任务中使用.net ksoap webservice并转到其他屏幕

Android 在asynchorinos任务中使用.net ksoap webservice并转到其他屏幕,android,web-services,android-asynctask,Android,Web Services,Android Asynctask,我是android新手。我试图在异步任务中通过ksoap使用.net WebService。但应用程序崩溃。我不知道问题出在哪里。这是我的代码。。。请帮助我 public class LoginActivity extends Activity { private String METHOD_NAME = "AuthenticateUser"; // our webservice method name private String NAMESPACE = "http://t

我是android新手。我试图在异步任务中通过ksoap使用.net WebService。但应用程序崩溃。我不知道问题出在哪里。这是我的代码。。。请帮助我

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;

        }
         }
        }
公共类LoginActivity扩展活动{
私有字符串方法\u NAME=“AuthenticateUser”//我们的Web服务方法名称
私有字符串命名空间=”http://tempuri.org/";
私有字符串SOAP_ACTION=”http://tempuri.org/AuthenticateUser";
//私有字符串SOAP_ACTION=”http://LocationBasedTaxiServices/UserWebServices.asmx/RegisterUser“;//命名空间+方法名
私有静态最终字符串URL=”http://192.168.0.45/LocationBasedTaxiServices/UserWebServices.asmx?WSDL“;//你
//////////////////////从R文件获取电子邮件amd密码////////////////
私人编辑文本登录\u电子邮件;
私人编辑文本登录密码;
私有字符串结果;
私人按钮登录;
私有文本视图寄存器文本视图;
字符串用户标识;
字符串密码;
字符串认证;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginbtn=(按钮)findViewById(R.id.btnLogin);
登录\电子邮件=(EditText)findViewById(R.id.l\e);
登录密码=(EditText)findViewById(R.id.l\p);
loginbtn.setOnClickListener(新视图.OnClickListener(){
//SoapObject请求=新的SoapObject(名称空间、方法名称);
公共void onClick(视图v){
user_id=login_email.getText().toString();
password=login_password.getText().toString();
新建asynLogin().execute();
}
});
registerTextView=(TextView)findViewById(R.id.link\u to\u寄存器);
registerTextView.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//切换到寄存器屏幕
Intent i=新Intent(getApplicationContext(),Register.class);
星触觉(i);
}
});
}
私有类asynLogin扩展了AsyncTask{
private final ProgressDialog=新建ProgressDialog(LoginActivity.this);
受保护的void onPreExecute(){
this.dialog.setMessage(“登录…”);
this.dialog.show();
}
受保护的空位背景(最终空位…未使用){
//身份验证=doLogin(用户id、密码);
身份验证=doLogin(用户id、密码);
return null;//不与ui交互!
}
受保护的void onPostExecute(void结果){
if(this.dialog.isShowing()){
this.dialog.disclose();
}
if(authentication.equals(true)){
Intent i=新的Intent(getApplicationContext(),MainActivity.class);
星触觉(i);
完成();
}否则{
Toast.makeText(getApplicationContext(),“用户名不存在”,Toast.LENGTH_LONG.show();
}
}
私有字符串doLogin(字符串用户id、字符串密码){
SoapPrimitive resultstring=null;
SoapObject请求=新的SoapObject(名称空间、方法名称);
请求.addProperty(“电子邮件”,用户id);
request.addProperty(“密码”,password);
SoapSerializationEnvelope soapenvelope=新的SoapSerializationEnvelope(soapenvelope.VER11);
soapenvelope.dotNet=true;
setOutputSoapObject(请求);
HttpTransportSE httptransport=新的HttpTransportSE(URL);
httptransport.debug=true;
试一试{
调用(SOAP\u操作,soapenvelope);
resultstring=(SoapPrimitive)soapenvelope.getResponse();
result=resultstring.toString();
登录_email.setText(结果);
Log.i(“myLogin”,resultstring.toString());
System.out.println(resultstring);
}捕获(SocketException例外){
login_email.setText(例如getMessage());
Log.e(“错误:”,“soapPrimitiveData()上的错误”+ex.getMessage());
例如printStackTrace();
}捕获(例外e){
Log.e(“错误:”,“soapPrimitiveData()上的错误”+e.getMessage());
e、 printStackTrace();
}
//返回结果字符串+“”;
返回结果;
}
}
}

请帮帮我。提前谢谢。从一个活动转到另一个活动时遇到问题,而webservice的响应与我想要的完全相同……

您正在调用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);
方法。在该方法内部,您可以调用finnish或任何您需要的东西,哦,作为异步任务构造函数中活动/接口的实例

private MyInterfaceWithOnAuthentificationMethod callbackObject;
public asynLogin(MyInterfaceWithOnAuthentificationMethod obj){
callbackObject = obj;
}

-重构您的类并给出一个大写名称,对于更短的接口,lol

您需要将
finish()
doInBackground()
移动到
onPOstExecute()
,如果条件不好,则替换此

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

为什么要在doInBackground()方法中调用
finish()
。?这只是一个尝试;在onPostExecute中。。。但它仍然不起作用。。。我走的方向对吗??为什么不起作用?你犯了什么错误@Faizahere in Log cat@Areader如果她获得数据,然后将数据移动到onPostExecute方法中的下一个活动,那么就不要这样做了。…@Fai