在android登录中的肥皂webservices

时间:2013-06-18 13:29:20

标签: android web-services android-asynctask android-ksoap2

我有一个Android应用程序。在这里,我必须使用soap webservices创建带有mysql连接的登录页面。

我的代码在android 2.2版本上运行良好。但它在android 4.0中无效。

所以我必须使用asynchronousTask。但我不知道asynchronousTask.please帮助我解决这些问题。

修改

我使用了以下代码:

public class AndroidLoginExampleActivity extends Activity {
private final String NAMESPACE = "http://ws.userlogin.com";
private final String URL = "http://111.223.128.10:8085/AndroidLogin/services/Login?wsdl";
private final String SOAP_ACTION = "http://ws.userlogin.com/authentication";
private final String METHOD_NAME = "authentication";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button login = (Button) findViewById(R.id.btn_login);
    login.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
new LongOperation().execute();

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

@Override
protected String doInBackground(String... aurl) {
// TODO Auto-generated method stub
loginAction();
return null;
   }

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

      protected void onPostExecute(String resultGot) {
    Dialog.dismiss();
     }
    }
    private void loginAction(){
   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    EditText userName = (EditText) findViewById(R.id.tf_userName);
    String user_Name = userName.getText().toString();
    EditText 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

  //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.VER11);
    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();

           TextView result = (TextView) findViewById(R.id.tv_status);
           result.setText(response.toString());

    }
    catch(Exception e){

    }
      });
   }

   }

现在也遇到了同样的错误:

在此之前:

现在我必须在目标版本上创建应用程序android 4.0意味着它在Android 2.2上运行良好。但它在android 4.0版本上无效。请检查我的代码并给我解决方案。

2 个答案:

答案 0 :(得分:3)

步骤1.请在现有的AndroidLoginExampleActivity中创建一个extends AsyncTask的课程,如下所示:

class LongOperation extends AsyncTask<String, Void, String> {
        private ProgressDialog Dialog = new ProgressDialog(OffersActivity.this);

    @Override
    protected String doInBackground(String... aurl) {
        // TODO Auto-generated method stub
        loginAction();
        return null;
    }

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

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

第2步。调用已实现服务器实现代码的方法loginAction();

步骤3.从您的活动的LongOperation AndroidLoginExampleActivity方法执行onCreate()课程,如下所示:

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

new LongOperation().execute();

}

步骤4.您只是想知道在doInBackground()中您无法直接在任何UI任务中执行文本视图,对于此问题,您必须进行runOnUiThread(new Runnable()然后制作所有UI的代码(在您的案例中为TextView)。请参阅下面的代码...

 runOnUiThread(new Runnable() {
                @Override
                public void run() {

    try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
               SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

               TextView result = (TextView) findViewById(R.id.tv_status);
               result.setText(response.toString());

        }
        catch(Exception e){

        }

    }
         });

您可以直接将此代码部分粘贴到loginAction()方法....

如果有任何混淆,请告诉我......

希望,现在您也可以在ICS和JellyBean中运行您的应用程序了......:)

答案 1 :(得分:2)

尝试使用以下代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button login = (Button) findViewById(R.id.btn_login);
  login.setOnClickListener(new View.OnClickListener() {

          public void onClick(View arg0) {
              AsyncCallWS task = new AsyncCallWS();
             task.execute(); 

         }
 });
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Log.i(TAG, "doInBackground");
       loginAction();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}

private void loginAction(){
....
...
 }