我在 AsyncTask 的 doInBackground () 中收到错误
I get the error in doInBackground () in AsyncTask
我在 asyncTask 的 doInBackground () 部分的 AsyncHttpClient 部分收到错误。
我怎样才能摆脱这个错误?
收到的错误行如下所示。
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;
}
错误
enter image description here
enter image description here
正如@Uday Nayak 所说,您不能在 doInBackground 方法上访问 UI 元素,它会导致崩溃(它 运行 不同的线程)。但是,如果您必须这样做,请将其包装在
runOnUiThread(() -> {
// to UI access
});
或创建处理程序以访问主线程以显示您的 toast
我在 asyncTask 的 doInBackground () 部分的 AsyncHttpClient 部分收到错误。 我怎样才能摆脱这个错误? 收到的错误行如下所示。
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;
}
错误 enter image description here
enter image description here
正如@Uday Nayak 所说,您不能在 doInBackground 方法上访问 UI 元素,它会导致崩溃(它 运行 不同的线程)。但是,如果您必须这样做,请将其包装在
runOnUiThread(() -> {
// to UI access
});
或创建处理程序以访问主线程以显示您的 toast