0

I want to connect Mysql database via webservice.

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

        public void onClick(View arg0) {
            Thread th = new Thread(new Runnable() {
                @Override
                public void run() {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new Authentication()
                                    .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

                        }
                    });
                }
            });
            th.start();

Authentication class for Webservice

    class Authentication extends AsyncTask<Void, Void, Void> {

    final String NAMESPACE = "http://aractakipyazilimlari.com";
    final String URL = "http://10.0.2.2:8080/IsbakApp/services/Login?wsdl";
    final String SOAP_ACTION = "http://aractakipyazilimlari.com/authentication";
    final String METHOD_NAME = "authentication";

    @Override
    protected Void doInBackground(Void... params) {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        String user_Name = userName.getText().toString();
        String user_Password = userPassword.getText().toString();


        PropertyInfo unameProp = new PropertyInfo();
        unameProp.setName("plaka");
        unameProp.setValue(user_Name);
        unameProp.setType(String.class);
        request.addProperty(unameProp);

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

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            result.setText(response.toString());

        } catch (Exception e) {

            Log.e("Login", e.toString());
        }

        return null;
    }

}

LogCat Error message

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

GrIsHu
  • 29,068
  • 10
  • 64
  • 102

1 Answers1

0

1/ You can't modify a View in a background thread, you need to do it on the UI thread which is possible by overriding onPostExecute(). This method takes as a parameter, a value returned by doInBackground(), in your case that would be: response.

Also, you will need to modify the signature of your AsyncTask:

class Authentication extends AsyncTask<Void, Void, String> {
}

Since The third type refers to the type of the value returned by doInBackground().

2/ As Piyush mentioned, you don't need to start a new Thread, as it is exactly what AsyncTask is about.

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158