1

enter image description here This is my code, I'm using volley to call a web service

private void sendRequest(){

    StringRequest stringRequest = new StringRequest(JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

Anything wrong, getting NullPointerException

URL:

https://www.bgr.ionidea.com/math_lms/webservice/rest/server.php?wstoken=840e7d1cb52ca12239b01926310e0c68&wsfunction=local_math_get_android_version&moodlewsrestformat=json

ShowJson method

 private void showJSON(String json){
        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        CustomList cl = new CustomList(this, ParseJSON.id,ParseJSON.app_version);
        listView.setAdapter(cl);
    }

Parse JSON class

public class ParseJSON {

    public static String[] id;
    public static String[] app_version;


    public static final String JSON_ARRAY = "result";
    public static final String KEY_ID = "id";
    public static final String KEY_APP_VERSION = "app_version";


    private JSONArray users = null;

    private String json;

    public ParseJSON(String json){
        this.json = json;
    }

    protected void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            users = jsonObject.getJSONArray(JSON_ARRAY);

            id = new String[users.length()];
            app_version = new String[users.length()];


            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
                id[i] = jo.getString(KEY_ID);
                app_version[i] = jo.getString(KEY_APP_VERSION);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

3 Answers3

1

Solution :

I found the Solution your id field you are taking in class is String and in response you will get the Integer value. That's why the error is occure.

enter image description here

Explantion :

In your response you get this "id":1 but actually you require this "id":"1".

Changing :

1.) change this public static String[] id; to public static Integer[] id;

2.) change this id[i] = jo.getString(KEY_ID); to id[i] = jo.getInt(KEY_ID);.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
1

I think it has to do something with your JSON. You first ask for a JSONObject. In this JSONObject you ask for a JSONArray named result. In this JSONArray you ask for 2 values: id and app_version. So I think you JSON needs to look something like this:

{
    "result": [
      {"id": "1", "app_version": "1"}
    ]
}

With some more users it would look something like:

{
  "result": [
      {"id": "1", "app_version": "1"},
      {"id": "2", "app_version": "1"},
      {"id": "3", "app_version": "2"}
    ]
}

OR

If you can't change the JSON you need to make some changes:

  1. Don't use a JSONObject at the start but start immediatly with the JSONArray like users = new JSONArray(json);
  2. Like @Ironman told you need to change your id to be an int[] instead of a String[] and use getInt() instead of getString().
Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
0

Your response is here:

[
  {
    "id": 1,
    "app_version": "1"
  }
]

Here , it is a list of json object. Object contains "id" that is integer and "app_version" is String. You can easily parse this response using Gson library. Here is sample code:

add this dependency in build.gradle file

compile 'com.google.code.gson:gson:2.2.4'

Now create a class like this:

import com.google.gson.annotations.SerializedName;
public class ResponseObject {

    @SerializedName("id")
    public Integer id;
    @SerializedName("app_version")
    public String app_version;

}

Now, you can parse your response using this way:

Type listType = new TypeToken<ArrayList<ResponseObject>>() {
                    }.getType();
 List<ResponseObject> yourClassList = new Gson().fromJson(jsonArray, listType);

Here, jsonArray is your response array.

Hope this will help you.

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87