0

Code I have for getting the JSON from the API

        String url = "https://api.open5e.com/weapons/?format=json";

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url.toString(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONObject tasksObject = jsonObject.getJSONObject("results");
                            for (Iterator<String> it = tasksObject.keys(); it.hasNext();){
                                String taskId = it.next();
                                JSONObject rObj = tasksObject.getJSONObject(taskId);

                                //get rows
                                String name = rObj.getString("name");
                                String weight = rObj.getString("weight");
                                String value = rObj.getString("cost");

                                Log.d(TAG, name);
                                Log.d(TAG, weight);
                                Log.d(TAG, value);

                                //Call setters setting values to correct values
                                lst list = new lst();
                                list.setlstName(name);
                                list.setLstweight(weight);
                                list.setLstValue(value);
                                lists.add(list);
                            }

Error I get from the following when I try and open the itemList page, this is where the name, weight and value are stored.

D/ITEMLIST: {"count":37,"next":null,"previous":null,"results":[{"name":"Club","slug":"club","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"1 sp","damage_dice":"1d4","damage_type":"bludgeoning","weight":"2 lb.","properties":["light"]},{"name":"Dagger","slug":"dagger","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"2 gp","damage_dice":"1d4","damage_type":"piercing","weight":"1 lb.","properties":["finesse","light","thrown (range 20/60)"]},{"name":"Greatclub","slug":"greatclub","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"2 sp","damage_dice":"1d8","damage_type":"bludgeoning","weight":"10 lb.","properties":["two-handed"]},{"name":"Handaxe","slug":"handaxe","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"5 gp","damage_dice":"1d6","damage_type":"slashing","weight":"2 lb.","properties":["light","thrown (range 20/60)"]},{"name":"Javelin","slug":"javelin","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"5 sp","damage_dice":"1d6","damage_type":"piercing","weight":"2 lb.","properties":["thrown (range 30/120)"]},{"name":"Light hammer","slug":"light-hammer","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"2 gp","damage_dice":"1d4","damage_type":"bludgeoning","weight":"2 lb.","properties":["light","thrown (range 20/60)"]},{"name":"Mace","slug":"mace","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"5 gp","damage_dice":"1d6","damage_type":"bludgeoning","weight":"4 lb.","properties":[]},{"name":"Quarterstaff","slug":"quarterstaff","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"2 sp","damage_dice":"1d6","damage_type":"bludgeoning","weight":"4 lb.","properties":["versatile (1d8)"]},{"name":"Sickle","slug":"sickle","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"1 gp","damage_dice":"1d4","damage_type":"slashing","weight":"2 lb.","properties":["light"]},{"name":"Spear","slug":"spear","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"1 gp","damage_dice":"1d6","damage_type":"piercing","weight":"3 lb.","properties":["thrown (range 20/60)","versatile (1d8)"]},{"name":"Crossbow, light","slug":"crossbow-light","category":"Simple Ranged Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"25 gp","damage_dice":"1d8","damage_type":"piercing","weight":"5 lb.","properties":["ammunition (range 80/320)","loading","two-handed"]},{"name":"Dart","slug":"dart","category":"Simple Ranged Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"5 cp","damage_dice":"1d4","damage_type":"piercing","weight":"1/4 lb.","properties":["finesse","thrown (range 20/60)"]},{"name":"Shortbow","slug":"shortbow","category":"Simple Ranged Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http://open5e.com/legal","cost":"25 gp","damage_dice":"1d6","damage_
W/System.err: org.json.JSONException: Value [{"name":"Club","slug":"club","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"1 sp","damage_dice":"1d4","damage_type":"bludgeoning","weight":"2 lb.","properties":["light"]},{"name":"Dagger","slug":"dagger","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"2 gp","damage_dice":"1d4","damage_type":"piercing","weight":"1 lb.","properties":["finesse","light","thrown (range 20\/60)"]},{"name":"Greatclub","slug":"greatclub","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"2 sp","damage_dice":"1d8","damage_type":"bludgeoning","weight":"10 lb.","properties":["two-handed"]},{"name":"Handaxe","slug":"handaxe","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"5 gp","damage_dice":"1d6","damage_type":"slashing","weight":"2 lb.","properties":["light","thrown (range 20\/60)"]},{"name":"Javelin","slug":"javelin","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"5 sp","damage_dice":"1d6","damage_type":"piercing","weight":"2 lb.","properties":["thrown (range 30\/120)"]},{"name":"Light hammer","slug":"light-hammer","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"2 gp","damage_dice":"1d4","damage_type":"bludgeoning","weight":"2 lb.","properties":["light","thrown (range 20\/60)"]},{"name":"Mace","slug":"mace","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"5 gp","damage_dice":"1d6","damage_type":"bludgeoning","weight":"4 lb.","properties":[]},{"name":"Quarterstaff","slug":"quarterstaff","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"2 sp","damage_dice":"1d6","damage_type":"bludgeoning","weight":"4 lb.","properties":["versatile (1d8)"]},{"name":"Sickle","slug":"sickle","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"1 gp","damage_dice":"1d4","damage_type":"slashing","weight":"2 lb.","properties":["light"]},{"name":"Spear","slug":"spear","category":"Simple Melee Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"1 gp","damage_dice":"1d6","damage_type":"piercing","weight":"3 lb.","properties":["thrown (range 20\/60)","versatile (1d8)"]},{"name":"Crossbow, light","slug":"crossbow-light","category":"Simple Ranged Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"25 gp","damage_dice":"1d8","damage_type":"piercing","weight":"5 lb.","properties":["ammunition (range 80\/320)","loading","two-handed"]},{"name":"Dart","slug":"dart","category":"Simple Ranged Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"5 cp","damage_dice":"1d4","damage_type":"piercing","weight":"1\/4 lb.","properties":["finesse","thrown (range 20\/60)"]},{"name":"Shortbow","slug":"shortbow","category":"Simple Ranged Weapons","document__slug":"wotc-srd","document__title":"Systems Reference Document","document__license_url":"http:\/\/open5e.com\/legal","cost":"25 gp"
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:101)
        at org.json.JSONObject.getJSONObject(JSONObject.java:624)
        at com.example.a5ENotes.itemlist$1.onResponse(itemlist.java:119)
        at com.example.a5ENotes.itemlist$1.onResponse(itemlist.java:112)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I console logged the response and that seems to be fine, it seems to break as soon as I get to. JSONObject tasksObject = jsonObject.getJSONObject("results");

So I thought maybe it was a problem with the API cause it pulls half of it, but I put the JSON from https://api.open5e.com/weapons/ into a validator and it was fine. Concluding that I'm just probably doing something wrong here.

1 Answer 1

1

"results" is JSONArray and not JSONObject. You would have to do something like this, to access results:

try
{
  JSONObject jsonObject = new JSONObject(response);
  JSONArray jsonArray = jsonObject.getJSONArray("results");
  for(int i=0; i < jsonArray.length(); i++)
  {
     JSONObject rObj = jsonArray.getJSONObject(i);

     String name = rObj.getString("name");
     String weight = rObj.getString("weight");
     String value = rObj.getString("cost");
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.