0

Beginner!

I have quiz app full code on Github that loads a List with four arguments from json (data was previously stored in xml):

  1. question
  2. image
  3. four possible answers presented in a radioGroup (sub-List)
  4. correct answer
   [{
        "question": "Who is the 'Modern Love' rock star singer?",
        "imageUrl": "https://postimg.cc/2VL1Y1jd",
        "answerOptions": [{
            "1": "Jaimie Hendrix",
            "2": "David Bowie",
            "3": "Jim Morrison",
            "4": "Elvis Presley"
        }],
        "correctAnswer": "David Bowie"
    }]

I'm getting the error

java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 118 path $[0].answerOptions[0]

and that's because I have declared all datatyped requested as strings while clearly I have an array of mixed types (an object and an array):

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
     if (response.isSuccessful()) {
          String string = response.body().string();
          Gson gson = new Gson();
          Type type = new TypeToken<List<Quiz>>(){}.getType();
          List<Quiz> list = gson.fromJson(string, type);

          // Save to database
          for (int i = 0; i < list.size(); i++) {
              Quiz quiz = list.get(i);
              quiz.save();
          }
...

I could implement the solution here in the bridge class:

class Quiz extends LitePalSupport {
    String question;
    String imageUrl;
    String [] answerOptions;
    String correctAnswer;

    Quiz(String question, String imageUrl, String [] answerOptions, String correctAnswer) {
        this.question = question;
        this.imageUrl = imageUrl;
        this.answerOptions = answerOptions;
        this.correctAnswer = correctAnswer;
    }
}

but that means I'll be sending a SubClass to the adapter view holder which is expecting String []

...
quizHolder.createRadioButtons(quiz.answerOptions);
...
void createRadioButtons(String[] answerOptions) {
    if (radioGroup.getChildAt(0) != null)
        radioGroup.removeAllViews();
    for (String s : answerOptions) {
        radioGroup.addView(createRadioButtonAnswerAndSetOnClickListener(s));
    }
}
...

Is there a way I could get the sub-list from json without creating a subclass?


Edit:

After the recommended change by @andy-turner I'm getting the error:

org.litepal.exceptions.LitePalSupportException: Attempt to invoke virtual method 'java.lang.Class[] java.lang.reflect.Constructor.getParameterTypes()' on a null object reference

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.celebrityquiz, PID: 6493
    org.litepal.exceptions.LitePalSupportException: Attempt to invoke virtual method 'java.lang.Class[] java.lang.reflect.Constructor.getParameterTypes()' on a null object reference
        at org.litepal.crud.DataHandler.query(DataHandler.java:154)
        at org.litepal.crud.QueryHandler.onFindAll(QueryHandler.java:123)
        at org.litepal.Operator.findAll(Operator.java:1117)
        at org.litepal.Operator.findAll(Operator.java:1082)
        at org.litepal.LitePal.findAll(LitePal.java:798)
        at com.example.celebrityquiz.MainActivity$1$2.run(MainActivity.java:115)
        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:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
...

probably invoked when I try to get the data from the databse:

List<Quiz> list = LitePal.findAll(Quiz.class);

And the answerOptions column isn't showing up either

enter image description here

axelmukwena
  • 779
  • 7
  • 24

2 Answers2

2

answerOptions is expected to be a String[], but you've specified it as Object[] (because of the {}):

    "answerOptions": [{
        ...
    }],

Make it an array of strings:

    "answerOptions": [
        "Jaimie Hendrix",
        "David Bowie",
        "Jim Morrison",
        "Elvis Presley"
    ],

PS - "Jimi", rather than "Jaimie".

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

my propo

-> change Json replace List (Objects or Map) to List Strings

package pl.jac.mija.gson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class QuizTestChangeJson {
  @Test
  public void changeJson() {
    //given
    String json = getNewJsonOneProposeOptions();
    Type type = new TypeToken<List<Quiz>>() {
    }.getType();
    //when
    List<Quiz> list = new Gson().fromJson(json, type);
    //then
    assertEquals(1, list.size());
    String correctAnswer = list.get(0).correctAnswer;
    assertEquals(correctAnswer, list.get(0).answerOptions[1]);
    assertEquals(1, Arrays.asList(list.get(0).answerOptions).indexOf(correctAnswer)); // key is string
  }

  @NotNull
  private String getNewJsonOneProposeOptions() {
    return "   [{\n" +
            "        \"question\": \"Who is the 'Modern Love' rock star singer?\",\n" +
            "        \"imageUrl\": \"https://postimg.cc/2VL1Y1jd\",\n" +
            "        \"answerOptions\": [\n" +
            "             \"Jaimie Hendrix\",\n" +
            "             \"David Bowie\",\n" +
            "             \"Jim Morrison\",\n" +
            "             \"Elvis Presley\"\n" +
            "        ],\n" +
            "        \"correctAnswer\": \"David Bowie\"\n" +
            "    }]";
  }
}

class Quiz {
  String question;
  String imageUrl;
  String[] answerOptions;
  String correctAnswer;
}

change Model Quiz to Json

the first version is with the key String -> QuizV2_String

the second version is with the key Long -> QuizV2_Long

I added a test to pull the correct answer key

 package pl.jac.mija.gson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class QuizTestChangeModel {
  @Test
  public void changeModelQuizKeyString() {
    //given
    String json = getJsonOriginal();
    Type type = new TypeToken<List<QuizV2_String>>() {
    }.getType();
    //when
    List<QuizV2_String> list = new Gson().fromJson(json, type);
    //then
    assertEquals(1, list.size());
    assertEquals("David Bowie", list.get(0).answerOptions.get(0).get("2")); // key is string
  }

  @Test
  public void changeModelQuizKeyLong() {
    //given
    String json = getJsonOriginal();
    Type type = new TypeToken<List<QuizV2_Long>>() {
    }.getType();
    //when
    List<QuizV2_Long> list = new Gson().fromJson(json, type);
    //then
    assertEquals(1, list.size());
    assertEquals("David Bowie", list.get(0).answerOptions.get(0).get(2L)); // key is string
  }

  @Test
  public void changeModelQuizFindAnswer() {
    //given
    String json = getJsonOriginal();
    Type type = new TypeToken<List<QuizV2_String>>() {
    }.getType();
    //when
    List<QuizV2_String> list = new Gson().fromJson(json, type);
    //then
    String correctAnswer = list.get(0).correctAnswer;
    String key = list.get(0).answerOptions.get(0).entrySet().stream().filter(x -> correctAnswer.equals(x.getValue())).map(Map.Entry::getKey).findFirst().orElse(null);
    assertEquals("2", key);
    assertEquals("David Bowie", list.get(0).answerOptions.get(0).get(key));
  }

  @NotNull
  private String getJsonOriginal() {
    return "   [{\n" +
            "        \"question\": \"Who is the 'Modern Love' rock star singer?\",\n" +
            "        \"imageUrl\": \"https://postimg.cc/2VL1Y1jd\",\n" +
            "        \"answerOptions\": [{\n" +
            "            \"1\": \"Jaimie Hendrix\",\n" +
            "            \"2\": \"David Bowie\",\n" +
            "            \"3\": \"Jim Morrison\",\n" +
            "            \"4\": \"Elvis Presley\"\n" +
            "        }],\n" +
            "        \"correctAnswer\": \"David Bowie\"\n" +
            "    }]";
  }
}


class QuizV2_String {
  String question;
  String imageUrl;
  List<Map<String, String>> answerOptions;
  String correctAnswer;
}

class QuizV2_Long {
  String question;
  String imageUrl;
  List<Map<Long, String>> answerOptions;
  String correctAnswer;
}
DEV-Jacol
  • 567
  • 3
  • 10
  • I have no experience with Unit Tests (assertEquals) thus it's difficult to determine which sections from the above code I'm supposed to extract and integrate into my project – axelmukwena Apr 06 '20 at 12:03