This is perfectly fine. It means that you're using memory, then its being freed by the GC. Its only a problem is either you go out of memory, or you see performance hiccups due to garbage collection. But its not anything you need to race to fix.
I have had the same issue. Your code will work, but if this hangs for too long, the app can get a App Not Responding error and close. It also defeats the purpose of using an AsyncTask.
The issue is that your AsyncTask contains a text view so it will block any other actions while executing. To fix this, make your AsyncTask static and pass it a reference to the Text view. Then store it in a WeakReference.
static class GetEmployee extends AsyncTask<Void,Void,String>{
WeakReference<TextView> textUserWeakReference;
WeakReference<TextView> textPassWeakReference;
GetEmployee(TextView textUser, TextView textPass) {
textUserWeakReference = new WeakReference<>(textUser);
textPassWeakReference = new WeakReference<>(textPass);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// loading = ProgressDialog.show(MainActivity.this,"Fetching...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// loading.dismiss();
TextView textUser = textUserWeakReference.get();
TextView textPass = textPassWeakReference.get();
if(textView != null && textPass != null)
showEmployee(s, textUser, textPass);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequestParam(DATA_URL,id);
return s;
}
}
private static void showEmployee(String json, TextView txtBoxUser, TextView txtBoxPassCode){
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
String name = c.getString(GET_BOXUSER);
String desg = c.getString(GET_PASSCODE);
txtBoxUser.setText(name);
txtBoxPasscode.setText(desg);
} catch (JSONException e) {
e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With