3

The code below tested on LG G3 and it worked fine. However when I tested it on a Samsung Galaxy S3/S2 doInBackground() is not called for some reason.

Code to check api:

  public void startBlat(String tosearch) {
    AsynctaskMovie asynctaskMovie = new AsynctaskMovie();
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
        asynctaskMovie.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,tosearch);
    }
    else {
        asynctaskMovie.execute(tosearch);
    }

The Asynctask code:

class AsynctaskMovie extends AsyncTask<String, String, ArrayList<Movie>> {

    JSONParser jsonParser = new JSONParser();
    private static final String SEARCH_URL = "http://www.omdbapi.com/?";


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        movieArrayList = new ArrayList();
        Log.i(getActivity().getCallingPackage(), "onPreExecute");
    }

    @Override
    protected ArrayList<Movie> doInBackground(String... args) {
        Log.i(getActivity().getCallingPackage(),"doInBackground");

        HashMap<String, String> params = new HashMap<>();
        params.put("s", args[0]);
        params.put("r", "json");
        JSONObject json = jsonParser.makeHttpRequest(SEARCH_URL, "GET", params);
        Log.i(getActivity().getCallingPackage(), json.toString());
        if (json != null) {
            try {
                if (json.getString("Response").equals("False")) {
                    return movieArrayList;
                }
            } catch (JSONException e) {
            }
            try {
                JSONArray jsonArray = json.getJSONArray("Search");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                    String movieid = jsonObject.getString(App.getInstance().IMDBimdbID);
                    if (!movieid.equals("null")) {
                        Movie movie = new Movie(movieid);
                        movieArrayList.add(movie);
                    }
                }
                jsonArray = new JSONArray();

                for (Movie movie : movieArrayList) {
                    params = new HashMap<>();
                    params.put("i", movie.getMovieid());
                    params.put("plot", "short");
                    params.put("r", "json");
                    JSONObject jsongetfullinfo = jsonParser.makeHttpRequest(SEARCH_URL, "GET", params);
                    if (jsongetfullinfo != null) {
                        jsonArray.put(jsongetfullinfo);
                        Log.i("", jsongetfullinfo.toString());
                    }
                }
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jObject = jsonArray.getJSONObject(i);
                    movieArrayList.get(i).updateFromIMDB(jObject);
                }
                for (Movie movie : movieArrayList) {
                    movie.setMovieposter(LoadFromUrl(movie.getPosterURL()));
                }
                return movieArrayList;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return movieArrayList;
    }

    @Override
    protected void onPostExecute(ArrayList<Movie> movieArrayList) {
        Log.i("ronen", "list size: " + movieArrayList.size());

        if (movieArrayList.size() > 0) {
            listView.setAdapter(new MovieAdapter(getActivity(), movieArrayList));
            listView.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(getActivity().getApplicationContext(), "No found", Toast.LENGTH_SHORT).show();
        }
    }

    private Bitmap LoadFromUrl(String theurl) {
        URL url = null;
        Bitmap bmp = null;
        try {
            url = new URL(theurl);
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (IOException e) {
        }
        return bmp;
    }
}

I have no idea what could solve this problem. Following the answers I read here it seems that the code should work, but not so.

  • ***Important Notice*** AsyncTask cannot work simultaneously. Only one task can run per time –  Feb 23 '16 at 10:51
  • When i start this Asynctask, there is no other Asynctask running. – Ronen Kryzel Feb 23 '16 at 11:06
  • 1
    @Kilanny of course it can. Please do talk about things you do not know about much. – Marcin Orlowski Feb 23 '16 at 11:07
  • @MarcinOrlowski Only in API 11 or above –  Feb 23 '16 at 11:10
  • @Kilanny No. You can have mutiple async tasks running at the same time on any API. If you do not know how to make if happen, learn, but do not spread false information it cannot be done. – Marcin Orlowski Feb 23 '16 at 11:13
  • Please see http://stackoverflow.com/a/4072832/4795214 and http://stackoverflow.com/a/13800208/4795214 –  Feb 23 '16 at 11:18
  • @RonenKryzel onPreExecute is not executed also? – guipivoto Feb 23 '16 at 11:26
  • Why you want to run your AsyncTask in parallel. It's not recommended i think you should review your desgin. Doc here http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor, Params...) – umerk44 Feb 23 '16 at 13:06

1 Answers1

0

Can't see anything suspicious.

I'd recommend running a very simple AsyncTask on a very simple app, make sure it works (if not, maybe it something to do with the phone, so try on an emulator to be sure)

Then change it step by step to resemble your code, and you'll see where the bug is.

G'Luck!

Zohar
  • 1,820
  • 1
  • 18
  • 16
  • Thanks, the final search link was: [link](http://www.omdbapi.com/??s=fast&r=json) and should be: [link](http://www.omdbapi.com/?s=fast&r=json) – Ronen Kryzel Feb 24 '16 at 00:42