0

我正在使用使用 KSOAP 的 Web 服务发送要存储在数据库中的详细信息。Web 服务没有任何问题,它可以正常工作。当我没有使用 AsyncTask 类时,此代码有效。我是 android 新手,这是我第一次尝试使用 AsyncTask 类,但它不起作用。我附上了日志猫错误,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 线程中工作,而 Non-UI 在 Non-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();

在 doInBackground() 内部,它不在 UI 线程中运行。这导致了问题。

尝试这个:

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

或移动Toast.makeText到 onPostExecute() 方法,该方法在 UI 线程中运行

于 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 回答