aj20010319 aj20010319 - 3 months ago 22
Android Question

AsyncTask not calling doInBackground

I have a button in my application which on click should run AsyncTask but the doInBackground method is not being called, I am trying to connect to my server using okHTTP inside AsyncTask, here is my code,

public class Home extends Fragment implements View.OnClickListener {
Button button;
public static final String TAG = "aj";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home, container, false);
button = (Button) v.findViewById(R.id.button);
button.setOnClickListener(this);
FirebaseMessaging.getInstance().subscribeToTopic("test");
FirebaseInstanceId.getInstance().getToken();
return v;
}

@Override
public void onClick(View v) {
sendRequest myTask = new sendRequest();
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB)
myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
myTask.execute();
}

private class sendRequest extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
Log.v(TAG, "doInBackground");
String token = FirebaseInstanceId.getInstance().getToken();

OkHttpClient client = new OkHttpClient();
RequestBody body = null;
if (token != null) {
body = new FormBody.Builder()
.add("Token", token)
.build();
}
Request request = new Request.Builder()
.url("https://www.example.com/aj20010319")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}


I don't know why the doInBackground method is not being called, the logger doesn't log anything regarding it. And if I am correct, it is setup correctly. I tried the solutions in other threads but none is working.

Answer

checked your code in my machine removing the complexities its working fine i pasted the code bellow. Two thing it can happen

  1. problem with build version . just call the myTask.execute();remove all the if else
  2. It might happen with the logcat . if possible make toast from the doInBackground
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
  1. If you are searching in the logcat it always searches by message not by tag so search by doInBackground not by aj
public class MainActivity extends Activity  implements View.OnClickListener{
Button button;
    @Override
    public void onClick(View arg0) {
    sendRequest myTask = new sendRequest();
    if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB)
     myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    else
     myTask.execute();

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(this);
    }



    private class sendRequest extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... params) 
        {
            int i=0;
            while(++i<100)
            {
                Log.i("www", "www");
            }

            return null;
        }
    }
}