2

私はAndroidの初心者です。非同期タスクでksoapを介して.net Webサービスを消費しようとしています.しかし、アプリがクラッシュします.問題のどこにあるのかわかりません.ここに私のコードがあります...親切に助けてください....

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

0

UIスレッドにある必要があるonPostExecuteを呼び出しています。無効な古いアクティビティを残したからです。

次のことを提案したいと思います: - 新しいアクティビティに移動 - 読み込みダイアログを表示 - 非同期タスクが終了したら: ダイアログを閉じます。

更新: そのアクティビティを続けたい場合は、コードを少し変更します。

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

方法。そして、そのメソッド ail 内で、フィンランド語または必要なものを呼び出し、Async タスク コンストラクターでのアクティビティ/インターフェイスのインスタンスとして呼び出します。

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

-クラスをリファクタリングして大文字の名前を付け、インターフェースの場合はもっと短くします、笑

于 2013-04-22T08:46:11.430 に答える