0

I'm trying to call webservice and set returned value to textview from service. But I get that fatal exception error when I click button. Where is my mistake?

Java Code:`

public class Login extends Activity {

private String SOAP_ACTION = "http://appointmentsystem.somee.com/HelloWorld";
private String METHOD_NAME = "HelloWorld";
private String NAMESPACE = "http://appointmentsystem.somee.com/";
private String URL = "http://appointmentsystem.somee.com/Service1.asmx";

private SoapObject request = null;
private SoapSerializationEnvelope envelope;
private HttpTransportSE androidHttpTransport;
private SoapPrimitive response;

private EditText loginEdtUser, loginEdtPassword;
private Button loginBtnEnter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    loginEdtUser = (EditText) findViewById(R.id.loginEdtUser);
    loginEdtPassword = (EditText) findViewById(R.id.loginEdtPassword);
    loginBtnEnter = (Button) findViewById(R.id.loginBtnEnter);

    loginBtnEnter.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AsyncCallWS task = new AsyncCallWS();
            task.execute();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Login();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    }

    @Override
    protected void onPreExecute() {
    }

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

}

public void Login() {
    try {
        request = new SoapObject(NAMESPACE, METHOD_NAME);

        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

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

            loginEdtUser.setText(response.toString());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }

}

Error:

{02-28 11:31:16.935: E/AndroidRuntime(4489): FATAL EXCEPTION: AsyncTask #3
02-28 11:31:16.935: E/AndroidRuntime(4489): Process: oziylmz.randevusistemi, PID: 4489
02-28 11:31:16.935: E/AndroidRuntime(4489): java.lang.RuntimeException: An error occured while executing doInBackground()
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.lang.Thread.run(Thread.java:841)
02-28 11:31:16.935: E/AndroidRuntime(4489): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.Handler.<init>(Handler.java:200)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.Handler.<init>(Handler.java:114)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.widget.Toast$TN.<init>(Toast.java:351)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.widget.Toast.<init>(Toast.java:101)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.widget.Toast.makeText(Toast.java:265)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at oziylmz.randevusistemi.Login.Login(Login.java:109)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at oziylmz.randevusistemi.Login$AsyncCallWS.doInBackground(Login.java:69)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at oziylmz.randevusistemi.Login$AsyncCallWS.doInBackground(Login.java:1)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)}
PPD
  • 5,660
  • 12
  • 52
  • 86
92Ozan_fb
  • 35
  • 5

3 Answers3

0

you get following error:

 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

this means you have try change UI in background, as i see your code you have try Toast data in catch of login method.

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

            loginEdtUser.setText(response.toString());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }

you can't update UI in background thread, you must handle error in onPostExecute or create Handler object and use that.

you can solve your issue with following way:

1- runOnUiThread Example

2- Handler

3- move to onPostExecute.

see following article for more info :

Android background processing with Handlers, AsyncTask and Loaders - Tutorial

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
0
 Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                Toast.LENGTH_SHORT).show();

this is causing exception because you cant touch ui elements in background processing.

Fix: Make toast in onPostexecute() method of async task than it will be executed on UI thread

H4SN
  • 1,482
  • 3
  • 24
  • 43
0

you want to use

 runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Login();
                    }
             });
Ram
  • 1,408
  • 13
  • 29