Issue
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
Solution
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
Answered By - Nanda Z
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.