Question

I am using this code:

  public void run() {
        handler.post(new Runnable() {
            public void run() {       
                try {
                    if (count==true)
                    {        
                        try 
                        {
                            r.stop();
                        } catch (Exception e) {}
                        tranca=false;
                        count=false; 

                        dlg.dismiss();
                        dlg.cancel();
                        dlg.dismiss();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );
                             //task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
                        } else {
                            new AsyncCaller().execute();
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                }
            }
        });
    }
}, 15000);

The problem is that i dont know how to make new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );

work. The problem is that asynctask changed the way it works after honeycomb, and i found this code. It works fine for Android Versions below Honeycomb, but in my jellybean it doesnt work. I am problably using new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ); in a wrong way. The asynctask is this:

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude1+","+longitude2+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {

        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here


        JSONObject ret = getLocationInfo(); 
        JSONObject location2;
        String location_string;
        try {
            location2 = ret.getJSONArray("results").getJSONObject(0);
            location_string = location2.getString("formatted_address");
            Log.d("test", "formattted address:" + location_string);
            StringRua = (" " + location_string);
        } catch (JSONException e1) {
            e1.printStackTrace();

        }

         StringSMS = (" Latitude " + StringLat + " Longitude " + StringLon + " Ad " + StringRua);   




        EditText Search01; 
        String String01;

        Search01 = (EditText) findViewById(R.id.Search01);
        String01 = Search01.getText().toString();  


        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(String01, null, StringSMS , null, null);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread

        pdLoading.dismiss();
    }

    }   

I know there is this exact same question in stackoverflow, but i tried a lot of the solution and no one seems to work for me. I just need to call asynctask approprely for a JellyBean device. Keep in mind that new AsyncCaller().execute(); works perfectly in devices below honeycomb. Thank You

EDIT: Still not working, it doesnt execute what is inside the dointhebackground in the Jelly Bean device (it show the Loading, so i know its sending to the AsynctTask). I've already tried to put outside the doingthebackground what need to be done putting in the Pre and Post, but i cant do this because of the "NetworkOnMainThreadException" problem, that just force close the program on new android versions.

No correct solution

OTHER TIPS

Copy the asynctask code here https://stackoverflow.com/a/9509184/2190244 and use it for both pre and post HC by calling execute if you would like to execute the async tasks on a different thread pool executor. For AsyncTasks in HC and above, the internal method that is used is a serial executor unless you specify your own executor and pass it to the execute method.

If you don't want to use the code, you can still call execute for both pre and post HC and it should work normally

at if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB), remove the = here. If you're BuildVersion is api 11, which it is, it will run serially already.

Try changing your targetSdk to the newest. The way AsyncTask are handled (serial/parallel) depends on the targetSdk. Read more about the pitfalls of AsyncTask here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top