Architecture for making Network Calls and parsing the JSON response Architecture for making Network Calls and parsing the JSON response json json

Architecture for making Network Calls and parsing the JSON response


Your current code can't be tested and breaks SOLID principles, your activity will soon became too huge to easily maintain. Try to avoid using Utils classes in case like that. Use IoC. So I recommend you to take a look at MVP/MVVM or any other presentation pattern.

  1. You should create separate function for each API call.
  2. Working with JSON should be definitely moved out from your activity. As you using OkHttp, I recommend you to use Retrofit over it. It's higher level which makes your API calls easies and you can use Gson/Jackson/Moshi or any other library as built-in adapter, so you won't care about serialising/marshalling Json string into Java objects, Retrofit adapters will take care of it.

Search for some articles about MVP pattern and take a look at this repository. Good luck.


I would recommend you Retrofit. You can init it with GsonConverterFactoryto deal with JSON and parse it by using pojoclass here. Here is an sample code:

 String BASE_URL = "https://YOUR API";            Retrofit.Builder builder = new Retrofit.Builder()                    .baseUrl(BASE_URL)                    .client(okHttpClient)                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                    .addConverterFactory(GsonConverterFactory.create());            apiService = builder.build().create(ApiService.class);


Although I understand your concerns in using a lot of o libraries I would definitely recommend Retrofit, it is the industry standard for a good reason (you can quickly google search why).

For parsing, you should use Moshi since you are already using Okhttp.

Another suggestion is to move your code to a more appropriate and scalable pattern such as MVP or MVVM. One thing you must take into account:

Fragments and/or Activities are not yours, they're Android owned objects, meaning they can be destroyed at any second.

Doing asynchronous work on these unstable objects is the recipe or bad code and memory leaks. Hence a good software pattern is super important in this environment.

I would recommend MVP if you are starting out, I've written a blog post about this where you can take your first steps:

AvengingAndroid MVP without RxJava or Dagger