title: JSONArray parsing using Retrofit Library and Display on ListView
body: <p>I am currently working on reading a JSONArray from a web service and populating it on a List View. Currently the app doesn't show me anything, or does it give any errors, so can anyone please tell me what I did wrong. Thank You in Advance.</p>
  
  <p>Here is my JSON File</p>
  
  <pre><code>{
      "products": [
          {
  
              "product_name": "wirless charger",
              "product_price": "9.99"
  
          },
          {
              "product_name": "phone cover ",
              "product_price": "4.99"
          },
          {
              "product_name": "otg cable ",
              "product_price": "2.99"
          }
      ]
  }
  </code></pre>
  
  <p>Here is my interface class</p>
  
  <pre><code>package com.example.productmanager.retrofit;
  
  import com.google.gson.JsonObject;
  
  import org.json.JSONArray;
  
  import java.util.List;
  
  import retrofit2.Call;
  import retrofit2.http.FormUrlEncoded;
  import retrofit2.http.GET;
  
  
  
  
   public interface Api {
  String BASE_URL = "http://192.168.101.97/productsmanager/products/";
  @GET("index.json")
  Call&lt;ProductWrapper&gt; getProducts();
  }
  </code></pre>
  
  <p>Here is my product POJO class</p>
  
  <pre><code>package com.example.productmanager.retrofit;
  
  import com.google.gson.annotations.Expose;
  import com.google.gson.annotations.SerializedName;
  
  
  public class Product {
  
  
      @SerializedName("product_name")
      @Expose
      private String product_name;
  
      @SerializedName("product_price")
      @Expose
      private String product_price;
  
  
      public String getProduct_name() {
          return product_name;
      }
  
  
      public String getProduct_price() {
          return product_price;
      }
  
  }
  </code></pre>
  
  <p>Here is my product wrapper POJO class</p>
  
  <pre><code>public class ProductWrapper {
  
  @SerializedName("products")
  @Expose
  private List&lt;Product&gt; products = null;
  
  public List&lt;Product&gt; getProducts() {
  return products;
  }
  
  public void setProducts(List&lt;Product&gt; products) {
  this.products = products;
  }
  
  }
  </code></pre>
  
  <p>Here is my main activity class</p>
  
  <pre><code>package com.example.productmanager.retrofit;
  
  import android.Manifest;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.net.Uri;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.ContextCompat;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.telephony.TelephonyManager;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  import com.google.gson.JsonParser;
  import com.google.gson.JsonSyntaxException;
  import com.google.gson.reflect.TypeToken;
  
  import org.json.JSONArray;
  
  import java.lang.reflect.Type;
  import java.util.ArrayList;
  import java.util.List;
  
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  public class MainActivity extends AppCompatActivity {
      private List&lt;product&gt; products = new ArrayList&lt;&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final ListView productListView = (ListView) findViewById(R.id.productListView);
  
          Gson gson = new GsonBuilder()
                  .setLenient()
                  .create();
  
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(Api.BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .build();
  
          Api api = retrofit.create(Api.class);
  
  
  
  Call&lt;ProductWrapper&gt; call = api.getProducts();
  
  call.enqueue(new Callback&lt;ProductWrapper&gt;() {
  
  
      @Override
      public void onResponse(Call&lt;ProductWrapper&gt; call, Response&lt;ProductWrapper&gt; response) {
          String productString = response.body().getProducts().toString();
  
          Type listType = new TypeToken&lt;List&lt;product&gt;&gt;() {}.getType();
          products = getproductList(productString, listType);
  
          final String[] productName = new String[products.size()];
  
          for(int i=0; i &lt; products.size(); i++)
          {
  
              productName[i] = products.get(i).getproduct_name();
  
          }
  
          productListView.setAdapter(new ArrayAdapter&lt;String&gt;(
                  getApplicationContext(),
                  R.layout.support_simple_spinner_dropdown_item,
                  productName
          ));
  
      }
  
      @Override
      public void onFailure(Call&lt;ProductWrapper&gt; call, Throwable t) {
          Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
      }
  });
  
              }
  
  
  
  
      }
  
      public static &lt;T&gt; List&lt;T&gt; getproductList(String productString, Type listType) {
          if (!isValid(productString)){
              return null;
  
          }
          return new Gson().fromJson(productString, listType);
      }
  
      private static boolean isValid(String productString) {
          try{
              new JsonParser().parse(productString);
              return true;
  
          }catch (JsonSyntaxException jse){
              return false;
          }
      }
  }
  </code></pre>
  
  <p>and my MainActivity xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.productManager.retrofit.MainActivity"&gt;
  
      &lt;ListView
          android:id="@+id/clientListView"
          android:cacheColorHint="#000"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"&gt;&lt;/ListView&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is my error log </p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                    Process: com.example.productmanager.retrofit, PID: 3628
                    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $[0]
                        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
                        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
                        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
                        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
                        at com.google.gson.Gson.fromJson(Gson.java:888)
                        at com.google.gson.Gson.fromJson(Gson.java:853)
                        at com.google.gson.Gson.fromJson(Gson.java:802)
                        at com.example.productmanager.retrofit.MainActivity.getProductsList(MainActivity.java:121)
                        at com.example.productmanager.retrofit.MainActivity$1.onResponse(MainActivity.java:87)
                        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                        at android.os.Handler.handleCallback(Handler.java:751)
                        at android.os.Handler.dispatchMessage(Handler.java:95)
                        at android.os.Looper.loop(Looper.java:154)
                        at android.app.ActivityThread.main(ActivityThread.java:6776)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
                     Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $[0]
                        at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
                        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
                        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) 
                        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) 
                        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) 
                        at com.google.gson.Gson.fromJson(Gson.java:888) 
                        at com.google.gson.Gson.fromJson(Gson.java:853) 
                        at com.google.gson.Gson.fromJson(Gson.java:802) 
                        at com.example.productmanager.retrofit.MainActivity.getProductsList(MainActivity.java:121) 
                        at com.example.productmanager.retrofit.MainActivity$1.onResponse(MainActivity.java:87) 
                        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70) 
                        at android.os.Handler.handleCallback(Handler.java:751) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:154) 
                        at android.app.ActivityThread.main(ActivityThread.java:6776) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 
  Device samsung-sm_j530f-5200fedcb478154bdisconnected, monitoring stopped.
  Application terminated.
  </code></pre>