0

我必须开发一个 android 应用程序。

在这里,我使用了以下代码。

  public class MainActivity extends Activity {
  String status;
  EditText username,userPassword;
    SoapSerializationEnvelope envelope;
    private final String NAMESPACE = "http://pricealert.com";
    private final String URL = "http://192.168.2.102:8085/PriceAlert/services/Login?wsdl";
    private final String SOAP_ACTION = "http://pricealert.com/authentication";
    private final String METHOD_NAME = "authentication";
    private String uName;
    HttpTransportSE androidHttpTransport;
     /**Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  username = (EditText) findViewById(R.id.tf_userName);
   userPassword = (EditText) findViewById(R.id.tf_password);

   Button login = (Button) findViewById(R.id.btn_login);
     login.setOnClickListener(new View.OnClickListener() {

   public void onClick(View arg0) {

      new LoginOperation().execute();

      }
     });
       }
      class LoginOperation extends AsyncTask<String, Void, String> {
      private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);

      @Override
  protected String doInBackground(String... aurl) {
 // TODO Auto-generated method stub
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
 username = (EditText) findViewById(R.id.tf_userName);
 String user_Name = username.getText().toString();
 userPassword = (EditText) findViewById(R.id.tf_password);
 String user_Password = userPassword.getText().toString();

 //Pass value for userName variable of the web service 
 PropertyInfo unameProp =new PropertyInfo();
 unameProp.setName("username");//Define the variable name in the web service method
 unameProp.setValue(user_Name);//set value for userName variable
 unameProp.setType(String.class);//Define the type of the variable
 request.addProperty(unameProp);//Pass properties to the variable it means 
 //Pass value for Password variable of the web service

 PropertyInfo passwordProp =new PropertyInfo();
 passwordProp.setName("password");
 passwordProp.setValue(user_Password);
 passwordProp.setType(String.class);
 request.addProperty(passwordProp);

  envelope = new SoapSerializationEnvelope(
         SoapEnvelope.VER12);
 envelope.setOutputSoapObject(request);
 androidHttpTransport = new HttpTransportSE(URL);
 runOnUiThread(new Runnable() {
     @Override
     public void run() {
 try{

        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        status = response.toString();
        TextView result = (TextView) findViewById(R.id.tv_status);
        result.setText(response.toString());

                  }
       catch(Exception e){
         }
     }
     });
     return null;
      }

     protected void onPreExecute() {
    Dialog.setMessage("Loading...");
    Dialog.show();
    }

    protected void onPostExecute(String resultGot) {
    Dialog.dismiss();
       }
       }
             }

如果我必须在android 2.2上添加应用程序,则意味着它可以完美运行并返回成功和失败值。

但是相同的代码在android 4.0版本上运行意味着没有收到任何(成功或失败)toast 消息,也没有在我的 logcate 窗口上收到任何错误???我的代码有什么问题???请为我提供一些解决方案来解决这些问题???

4

1 回答 1

1

您正在 UI 线程上调用 web 服务。这是一个严格的模式违规。您不应该在 UI 线程上执行数据库或网络操作。较新版本的 Android 对此更加严格,这就是它适用于其他版本的原因。默认情况下,doInBackground() 中的代码在单独的线程上运行,因此您无需在 UI 线程上运行任何内容。

要在调用后更新 UI,请使用Handler

于 2013-06-20T13:26:06.343 回答