0

I get the error in the AsyncHttpClient section located in the doInBackground () section in asyncTask. How can I get rid of the error? The error received line is indicated below.

Error: An error occured while executing doInBackground ()

public class BackgroundTask2 extends AsyncTask<String,Void,String> {

        protected Void doInBackground(String... params) {

            Bundle veri = getIntent().getExtras();
            sorguYil = veri.getString("yil");
            sorguAy = veri.getString("tarih");

            sorguAy2 = sorguAy.replace(" ", "");
            sorgu = "ehliyet" + sorguYil + sorguAy2;
            s = kelimeGetir(sorgu); // I am getting the error here
            return s;}

protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);

        if (aVoid.equals("internet")){
            Toast.makeText(sorularActivity.this, "Lütfen internet bağlantınızı kontrol edin!", Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();
            Intent intent = new Intent(getApplicationContext(),SinavSecimiActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);

        }else if (aVoid.equals("onay")){
            progressDialog.dismiss();
        }

    }

kelimeGetir(sorgu)

private String kelimeGetir(String sorgu) {

        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("sorgu", sorgu);
        client.post("https://www....php", params, new TextHttpResponseHandler()
//****ERROR {

            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                fnDu = "internetEr";

            }

            public void onSuccess(int statusCode, Header[] headers, String responseString) {

                try {

                    txtSoruSayisi.setText(soruSayisi + " / 50");

                    JSONObject object = new JSONObject(responseString);
                    JSONArray array = object.getJSONArray("soruVeri");
                    JSONObject sorular = array.getJSONObject(sayac);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
          fnDu = "onay";
        });

     return fnDu;

    }

ERROR enter image description here

enter image description here

  • 1
    Please [edit] your question to provide the complete [stack trace](https://stackoverflow.com/a/23353174). – Mike M. Jan 30 '19 at 23:33
  • I do not understand. I don't know exactly English.I tried to explain the error myself. – Markos Bayard Jan 30 '19 at 23:43
  • 1
    He means you have to get the [stack trace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) (from the logcat tab, given you're using Android Studio), read through it to find what the _exact_ error message is, then include the stack trace in your question. "_I tried to explain the error myself"_, well, it's usually more helpful to see the actual error message. – Gino Mempin Jan 30 '19 at 23:48
  • 1
    remove Toast from onFailure(). you can't call UI element from doInBackground.for that massage you can use post method of async task – Uday Nayak Jan 31 '19 at 06:04

1 Answers1

0

As @Uday Nayak said, you can't access UI element on doInBackground Method, it will cause crash (It running with different thread). But if you have to do it, wrap it on

runOnUiThread(() -> {
            // to UI access
        });

or create Handler to access main Thread to showing your toast

Nanda Z
  • 1,604
  • 4
  • 15
  • 37
  • I did what you said. The error is resolved. But I didn't get the result I wanted – Markos Bayard Jan 31 '19 at 11:09
  • So, what is your new result? It might be new issue – Nanda Z Jan 31 '19 at 11:31
  • Actually, that's not a problem. I want progressDialog to close after operations in kelimeGetir() is reflected on activity screen. However, progressDialog closes before the operations are reflected on the screen. Some pictures and articles that need to be installed are closing progressDialog when they are loaded. – Markos Bayard Jan 31 '19 at 12:16
  • Perhaps this [link](https://medium.com/@ankit.sinhal/understanding-of-asynctask-in-android-8fe61a96a238) could help you to understand Asynctask. I think you can do it on other method – Nanda Z Jan 31 '19 at 14:15