0

I am relatively new to retrofit and I have an issue in parsing some string arrays which are a part of a JSON response.

This is the JSON response.

{
"positive": [
    "Relaxed",
    "Uplifted",
    "Hungry",
    "Sleepy",
    "Tingly"
],

"medical": [
    "Eye Pressure",
    "Insomnia",
    "Stress",
    "Fatigue",
    "Headaches"
]
}

How do I approach this?

Thanks in advance :)

Sivan
  • 25
  • 2
  • 6
  • Create [POJO class](http://www.jsonschema2pojo.org/) then use this link: https://stackoverflow.com/questions/42623437/parse-json-array-response-using-retrofit-gson – ʍѳђઽ૯ท Oct 16 '18 at 15:55
  • `List` maybe? – Ayush Gupta Oct 16 '18 at 15:55
  • Create a POJO class which contains two list – Aymen Ragoubi Oct 16 '18 at 16:00
  • `class myJsonResponse(){ List positive = new List; List medical = new List; //getters and setters }` – Leonardo Velozo Oct 16 '18 at 16:04
  • My POJO : public List getPositive() { return positive; } – Sivan Oct 16 '18 at 16:11
  • My MainActivity.java `public void onResponse(Call> call, Response> response) { List basicDetailsList2 = response.body(); List positiveTraitsArray = basicDetailsList2.get(checknum).getPositive(); Log.d("Positive array", "onResponse: " + positiveTraitsArray); } @Override public void onFailure(Call> call, Throwable t) { Log.d("Error strainCall2 : ", "onFailure: " + t); } });` – Sivan Oct 16 '18 at 16:11
  • This is what my logcat tells me : `onFailure: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $` – Sivan Oct 16 '18 at 16:13

2 Answers2

0

You need to create POJO class like below

public  class  ExampleResponse  {

private  List < String >  positive  =  null;
private  List < String >  medical  =  null;

public  List < String >  getPositive() {
    return  positive;
}

public  void  setPositive(List < String >  positive) {
    this.positive  =  positive;
}

public  List < String >  getMedical() {
    return  medical;
}

public  void  setMedical(List < String >  medical) {
    this.medical  =  medical;
}

}

It's working perfectly.

Shohel Rana
  • 2,332
  • 19
  • 24
  • Trying this only for positive now. My POJO : ` private List< String > positive = null; public List getPositive() { return positive; }` – Sivan Oct 16 '18 at 18:33
  • Main Activity : ` @Override public void onResponse(Call> call, Response> response) { List basicDetailsList2 = response.body(); List positiveTraitsArray = basicDetailsList2.get(0).getPositive(); Log.d("Positive array", "onResponse: " + positiveTraitsArray); } I still get the java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ error :/ – Sivan Oct 16 '18 at 18:33
  • Yep. @Shashanth's and your answer worked. Just had to make slight changes to my pojo. Had to make sure I had a List postive. – Sivan Oct 18 '18 at 13:06
0

By looking into your comments I assume that your POJO class is proper but the way you're using it with Retrofit call is wrong. Make sure that the POJO class is like below,

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Foo {

    @SerializedName("positive")
    @Expose
    private List<String> positive = null;
    @SerializedName("medical")
    @Expose
    private List<String> medical = null;

    public List<String> getPositive() {
        return positive;
    }

    public List<String> getMedical() {
        return medical;
    }

}

And use above POJO class like this.

Your API interface

import retrofit2.Call;
import retrofit2.http.GET;

public interface FooApiInterface {

    /* pass POJO class to Call<> */

    @GET("request/url")
    Call<Foo> getFoo(/* parameters for the request if there any */);

    /* other api calls here */
}

Next use API interface

FooApiInterface client = ...;  // initialization

Call<Foo> fooCall = client.getFoo();

fooCall.enqueue(
         new Callback<Foo>() {
              @Override
              public void onResponse(@NonNull Call<Foo> call, @NonNull Response<Foo> response) {
                 if (response.isSuccessful()) {
                     List<String> positiveList = response.body().getPositive();
                     List<String> medicalList = response.body().getMedical();
                 }
              }

              @Override
              public void onFailure(@NonNull Call<Foo> call, @NonNull Throwable t) {
                     Log.e("error", "API Error ", t);
              }
    }
);

Notice that, here I am using Foo POJO class as parameter to Call instead of List<Strain> (like used in your code). If you modify the code as said above, you can get rid of the error

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ error

For demonstration purpose I used Foo example. Change it according to your requirements.

Shashanth
  • 4,995
  • 7
  • 41
  • 51