0

When the URL is ran manually in browser, this JSON is returned.

   {
  "error": false,
  "0": {
    "question": "Using the information that 6.7 × 52 = 348.4, Find the value of: 6.7 × 520",
    "useranswer": "3484",
    "correctanswer": "3484",
    "correct": "1"
  },
  "1": {
    "question": "Jane drives 50mph. London is 350 miles away. How long will it take?",
    "useranswer": "5",
    "correctanswer": "7",
    "correct": "0"
  },
  "2": {
    "question": "74*3?",
    "useranswer": "222",
    "correctanswer": "222",
    "correct": "1"
  },
  "3": {
    "question": "39+31?",
    "useranswer": "70",
    "correctanswer": "70",
    "correct": "1"
  }
}

The code is as follows:

public List<String> GetTestResultsFromUserID(Integer userID){
    BufferedReader bufferedReader = null;
    try {
        URL url = new URL(AppConfig.Results_URL + "?userid=" + userID);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty ("Authorization", "Basic Z2FycmV0dGg6ZnJBc3Rpbmc0");
        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String result;                    
        result = bufferedReader.readLine();
       return ProcessResultSetFromDatabase(result);
    }catch(Exception e){
        Log.d("Exception in try", "Exception" + e.toString());
        return null;
    }       
}

And then the result is processed here:

private List<String> ProcessResultSetFromDatabase(String result){
    List<String> resultSet = new ArrayList<String>();
    try{
        JSONObject jObj = new JSONObject(result);
        boolean error = jObj.getBoolean("error");       

        if (!error){ 
            for (int i=0; i<48; i++){
                JSONObject rSet = jObj.getJSONObject(Integer.toString(i));
                resultSet.add("Q: "+rSet.getString("question")+" Correct Ans: "+rSet.getString("correctanswer")+" Given Ans: "+rSet.getString("useranswer")+" Correct:"+(rSet.getString("correct")));                                   
            }     
        }else{
            resultSet.add("No results at the moment");
        }           
    }catch(JSONException e){
        e.printStackTrace();
    }               
    return resultSet;
}

Note: The result passed to ProcessResultSetFromDatabase seems to be null when passed.

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
bananabreadbob
  • 369
  • 2
  • 10
  • 26

2 Answers2

0

Your JSON output is in "0":{"question": String and you pass integer, So you need to convert int to string.

 for (int i=0; i<48; i++){


int tmpInt = i;
String str= String.valueOf(i);

                JSONObject rSet = jObj.getJSONObject(str);
                resultSet.add("Q: "+rSet.getString("question")+" Correct Ans: "+rSet.getString("correctanswer")+" Given Ans: "+rSet.getString("useranswer")+" Correct:"+(rSet.getString("correct")));                                   
            }  
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

You should parse your data using Iterator.

 JSONObject jObj = new JSONObject(result);
 if (jObj.length() > 0) {

                    Iterator<String> keys = jObj.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        JSONObject json= jObj.optJSONObject(key);

                        Log.e("json", json.toString());

                        if (json!= null) {
                        resultSet.add("Q: "+json.getString("question")+" Correct Ans: "+json.getString("correctanswer")+" Given Ans: "+json.getString("useranswer")+" Correct:"+(json.getString("correct")));                                   
                    }
                }
  }
Piyush
  • 18,895
  • 5
  • 32
  • 63