2

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

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;

        }
         }
        }

请帮助我。提前致谢。面对从一个活动到另一个活动的问题,而 web 服务的响应与我想要的完全一样......

4

2 回答 2

1

您需要finish()doInBackground()to移动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(); 
}
于 2013-04-22T12:12:46.927 回答
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);

方法。在该方法中,您可以调用芬兰语或您需要的任何内容哦,并且作为异步任务构造函数中的活动/接口的实例。

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

-重构你的类并给出一个大写的名称,并且接口更短,哈哈

于 2013-04-22T08:46:11.430 回答