-3

I am creating an app which uses REST API, for this I am using Okhttp API by square, and I am facing a challenge.

Earlier I was getting below error

E/WindowManager: android.view.WindowLeaked: Activity com.example.pawadube.helloworld.MainActivity has leaked window DecorView@3cc4f3[Connecting] that was originally added here
                                                                                at android.view.ViewRootImpl.<init>(ViewRootImpl.java:417)
                                                                                at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)

By referring this question, I added the code in the onDestroy method to point the progress dialog. And now I get below error

    E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
                                                                             Process: com.example.pawadube.helloworld, PID: 31149
                                                                             java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                 at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                 at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                 at java.lang.Thread.run(Thread.java:761)
                                                                              Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                                 at android.os.Handler.<init>(Handler.java:200)
                                                                                 at android.os.Handler.<init>(Handler.java:114)
                                                                                 at android.widget.Toast$TN.<init>(Toast.java:346)
                                                                                 at android.widget.Toast.<init>(Toast.java:101)
                                                                                 at android.widget.Toast.makeText(Toast.java:260)
                                                                                 at com.example.pawadube.helloworld.MainActivity$LoginServerTest.doInBackground(MainActivity.java:172)
                                                                                 at com.example.pawadube.helloworld.MainActivity$LoginServerTest.doInBackground(MainActivity.java:130)
                                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                             

On the button click I have added below code

    new LoginServerTest().execute(username, password, url);


class LoginServerTest extends AsyncTask<String, Integer, String[]> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDoalog.show();
    }

    @Override
    protected void onPostExecute(String[] strings) {
        super.onPostExecute(strings);
        progressDoalog.dismiss();
    }

    @Override
    protected String[] doInBackground(String... strings) {
        String username = strings[0];
        String password = strings[1];
        String url = strings[2];
        Log.d("Username: ", username);
        Log.d("Password: ", password);
        Log.d("URL--> ", url);

        String credential = Credentials.basic(username, password);
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("authorization", credential)
                .addHeader("content-type", "application/json")
                .build();

        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String body = response.body().string();
                String code = response.code() + "";
                String[] output = {code, body};
                Toast.makeText(MainActivity.this, "Username/Password is correct." + response.body().string(), Toast.LENGTH_LONG).show();
                Log.d("Responce-->", response.body().string());
                return output;
            } else {
                Toast.makeText(MainActivity.this, "Username/Password is correct.", Toast.LENGTH_LONG).show();
                Log.d("Responce-->", response.body().string());
                return null;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}

Please help me, I'm stuck.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pawan Dubey
  • 192
  • 3
  • 15

1 Answers1

0

You can't call interact with Toast on a background thread. Move your Toast.makeText().show() code to onPostExecute().

cwbowron
  • 1,015
  • 6
  • 10