0

KSOAP を使用した Web サービスを使用して、データベースに保存する詳細を送信しています。正常に動作する Web サービスに問題はありません。このコードは、AsyncTask クラスを使用していないときに機能しました。私はアンドロイドが初めてで、AsyncTask クラスを使用しようとしたのはこれが初めてで、機能しません。ログ cat エラーを添付しました。doinbackround メソッドに何か問題があります。私は何を間違っていますか?助けてください

public class Registration extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/register";
private static final String OPERATION_NAME = "register";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";
Button sqlRegister, sqlView;

EditText  sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sqlFirstName = (EditText) findViewById(R.id.etFname);
sqlLastName = (EditText) findViewById(R.id.etLname);
sqlEmail = (EditText) findViewById(R.id.etEmail);
sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);

sqlUsername = (EditText) findViewById(R.id.etUsername);
sqlPassword = (EditText) findViewById(R.id.etPwd);

sqlRegister = (Button) findViewById(R.id.bRegister);
sqlRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        switch (v.getId()){
        case R.id.bRegister:
        new LongOperation().execute("");
        break;
      }
     }
    });
}

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
         String firstname = sqlFirstName.getText().toString();
         String lastname = sqlLastName.getText().toString();
         String emailadd = sqlEmail.getText().toString();
         String number = sqlMobileNumber.getText().toString();
         String loc = sqlCurrentLocation.getText().toString();
         String uname = sqlUsername.getText().toString();
         String pwd = sqlPassword.getText().toString();

         SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
         Request.addProperty("fname", String.valueOf(firstname));
         Request.addProperty("lname", String.valueOf(lastname));
         Request.addProperty("email", String.valueOf(emailadd));
         Request.addProperty("num", String.valueOf(number));
         Request.addProperty("loc", String.valueOf(loc));
         Request.addProperty("username", String.valueOf(uname));
         Request.addProperty("password", String.valueOf(pwd));
         Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true;
         envelope.setOutputSoapObject(Request);
         HttpTransportSE httpTransport  = new HttpTransportSE(SOAP_ADDRESS);
         try
         {
            httpTransport.call(SOAP_ACTION, envelope);
             SoapObject response = (SoapObject)envelope.getResponse();
             int result =  Integer.parseInt(response.getProperty(0).toString());
             if(result == '1')
             {
                 return "Registered";
             }
             else
             {
                 return "Not Registered";
             }
         }catch(Exception e){
            e.printStackTrace();
        }
         return null;

    }      

    @Override
    protected void onPostExecute(String result) {
        if(result=="Registered")
        {
        Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
        }
        else if(result =="Not Registered")
        {
        Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
    }   
 }

ここに画像の説明を入力

////編集済み

public class Registration extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/register";
private static final String OPERATION_NAME = "register";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";
Button sqlRegister, sqlView;

EditText  sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sqlFirstName = (EditText) findViewById(R.id.etFname);
sqlLastName = (EditText) findViewById(R.id.etLname);
sqlEmail = (EditText) findViewById(R.id.etEmail);
sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);

sqlUsername = (EditText) findViewById(R.id.etUsername);
sqlPassword = (EditText) findViewById(R.id.etPwd);

sqlRegister = (Button) findViewById(R.id.bRegister);
sqlRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        switch (v.getId()){
        case R.id.bRegister:
        new LongOperation().execute("");
        break;
      }
     }
    });
}

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
         String firstname = sqlFirstName.getText().toString();
         String lastname = sqlLastName.getText().toString();
         String emailadd = sqlEmail.getText().toString();
         String number = sqlMobileNumber.getText().toString();
         String loc = sqlCurrentLocation.getText().toString();
         String uname = sqlUsername.getText().toString();
         String pwd = sqlPassword.getText().toString();

         SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
         Request.addProperty("fname", String.valueOf(firstname));
         Request.addProperty("lname", String.valueOf(lastname));
         Request.addProperty("email", String.valueOf(emailadd));
         Request.addProperty("num", String.valueOf(number));
         Request.addProperty("loc", String.valueOf(loc));
         Request.addProperty("username", String.valueOf(uname));
         Request.addProperty("password", String.valueOf(pwd));

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true;
         envelope.setOutputSoapObject(Request);
         HttpTransportSE httpTransport  = new HttpTransportSE(SOAP_ADDRESS);
         Log.d("work","work");
         try
         {
            httpTransport.call(SOAP_ACTION, envelope);
             SoapObject response = (SoapObject)envelope.getResponse();
             int result =  Integer.parseInt(response.getProperty(0).toString());
             if(result == 1)
             {
                 Log.d("reg","reg");
                 return "Registered";
             }
             else
             {
                 Log.d("no","no");
                 return "Not Registered";
             }
         }catch(Exception e){
            e.printStackTrace();
        }
         return null;

    }      

    @Override
    protected void onPostExecute(String result) {
        Log.d("tag","onpost");
        if(result!=null)
        {

            if(result.equals("Registered"))
                {
                    Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
                }
            else if(result.equals("Not Registered"))
                {
                    Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
                }
        }
        else
        {
            Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

    }   

}

4

4 に答える 4

0

を使用して文字列を比較しないでください==

protected void onPostExecute(String result) {
        if(result=="Registered")

これが、予期しない動作の背後にある理由である可能性があります。次のようなものを使用してみてください

result.equals("registered")

これを読む

于 2012-07-11T05:06:40.687 に答える
0

これを試して....

  1. 常に UI 作業を UI スレッドに保持し、非 UI 作業を非 UI スレッドに保持します。

  2. doInBackground は非 UI スレッドであるため、これから UI スレッドに投稿することはできません。

  3. Toast ステートメントは doInBackgroud の問題です。UI スレッドで動作している postExecute に移動します。

    Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
    
于 2012-07-11T04:35:48.893 に答える
0

あなたが呼んでいる

Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();

UIスレッドで実行されていないdoInBackground()内。それが問題の原因です。

これを試して:

runOnUiThread(new Runnable() {
    public void run() {
         Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
    }
});
}

またはToast.makeText、UI スレッドで実行される onPostExecute() メソッドに移動します。

于 2012-07-11T04:35:55.583 に答える
0

これは、doInBackground() で Toast を呼び出しているためです。doInBackground のどの UI でも looper() 例外が発生します。AsynTask の PostExecute() で UI を次のように作成してみてください。

@Override
protected void onPostExecute(String result) {

 Toast.makeText(Registration.this, "You have been registered Successfully",Toast.LENGTH_LONG).show();

}
 }

それはあなたに役立つかもしれません..

于 2012-07-11T04:40:21.877 に答える