2

When i call an rest api from android app it will send response as a JSON array which contains more than 5000 objects. While reading and parsing the response entity using for loop it is taking too much time and going unresponsive.

JSONArray list = new JSONArray(EntityUtils.toString(response.getEntity()));

Here is sample of the JSON:

[["aa@gmail.com",true],["bb@gmail.com",true],["cc@gmail.com",true],["dd@gmail.com",true],["ee@gmail.com",true],["ff@gmail.com",true],["gg@gmail.com",true],["hh@gmail.com",true]]

Please help me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manju
  • 21
  • 7
  • Could you please provide sample of the JSON as well so one can have better overview of data structure. – hris.to Jan 18 '19 at 07:02
  • @hris.to this is my sample JSON response array [["aa@gmail.com",true],["bb@gmail.com",true],["cc@gmail.com",true],["dd@gmail.com",true],["ee@gmail.com",true],["ff@gmail.com",true],["gg@gmail.com",true],["hh@gmail.com",true]] – Manju Jan 18 '19 at 07:06
  • Network operation must be in background thread, why still unresponsive? Do you handle JSON in onPostExecution ? – AIMIN PAN Jan 18 '19 at 08:16

3 Answers3

1

You can use a background thread, which is easy to accomplish with AsynTask, write your expensive code inside doInBackground

private static class BackgroundTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    JSONArray list = new JSONArray(EntityUtils.toString(response.getEntity()));
}

@Override
protected void onPostExecute(String html) {
}
}

Also try to parse json with something else, for example gson is much faster for certain type of data.

Feed feed = getGson().fromJson(json, Feed.class);

Anyway it's not a good practice to return 5000 objects at once, your API should be improved to handle pagination and return from 10 up to 100 items per request depending on your needs.

Minas
  • 1,422
  • 16
  • 29
  • It is already in background AsyncTask only and response array does not contains any key value pair of data so i am using index – Manju Jan 18 '19 at 06:57
  • @Manju I see, but still there is a way to use gson with a trick, check out the answer here: https://stackoverflow.com/questions/18525283/gson-parsing-string-that-has-no-key-value-pairs – Minas Jan 18 '19 at 07:03
  • `long time = System.currentTimeMillis(); ... Log.d("TAG", "took: " + (System.currentTimeMillis() - time));` surround both ways of parsing inside this code to understand which one is faster for your specific use case – Minas Jan 18 '19 at 07:05
  • Use a Service + Thread or Networking library (Volley). The Json will be fetched beyond the lifecycle of Activity – Rohit Singh Jan 18 '19 at 07:09
0

First of all you should do all these tasks in the background thread and while parsing if possible use HashMap<Key,Value> it has methods like hashmap.get("key") which will get you the values without using for loop or you can use arraylist also from which you can also obtain values without using for loop.

theanilpaudel
  • 3,348
  • 8
  • 39
  • 67
0

The easiest and most commonly used approach to this problem is to use pagination. Simply put - you request first 50 or 100 objects and display them, then the next and so on. The concept is that usually the end user do not need all the 5000 objects at once, so it is acceptable to receive them whenever needed.

If for some business/logic reasons you need to handle all 5000 objects at once, you may think of another communication format - for example protobuf or XML with SAX parser.

In both above cases you need to adjust the rest api that serves the data along with the client. If you have no control over the back-end, you may try to use Android Profiler to inspect exactly which parts of the parsing are slowest and optimise them on the client(android) side.

hris.to
  • 6,235
  • 3
  • 46
  • 55
  • any examples on protobuf? – Manju Jan 18 '19 at 06:51
  • The link I've provided has examples for every language. Yet, protobuf has some (rather steep) learning curve. Are you sure you cannot do it with pagination? Also I;ve updated my answer to include XML as an option as well. – hris.to Jan 18 '19 at 07:00