0

I'm trying to update my old application which is basically a shared shopping list. It was previously linked to my schools website and database, but I have reverse engineered it due to not having access anymore. The app was originally created for sdk 23 Problem is it does not seem to populate the JSONArray using this api.
Code goes as follows:

//MainActivity.java
@Override
    protected Long doInBackground(String... params) {
        HttpsURLConnection connection = null;
        try {
            URL itemListURL = new URL(itemList_URI);
            connection = (HttpsURLConnection) itemListURL.openConnection();
            connection.connect();
            int status = connection.getResponseCode();
            Log.d("HTTPS GET", "status " + status);
            if (status == HttpsURLConnection.HTTP_OK){
                InputStream is = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String responseString;
                StringBuilder sb = new StringBuilder();
                while ((responseString = reader.readLine()) != null) {
                    sb = sb.append(responseString);
                }
                String itemData = sb.toString();
                itemArrayList = Item.createItemList(itemData);
                .......
//Item.java
//Create ItemList
public static ArrayList<Item> createItemList(String jsonItemsString)
        throws JSONException, NullPointerException {
    ArrayList<Item> itemList = new ArrayList<Item>();

    JSONObject jsonData = new JSONObject(jsonItemsString);
    JSONArray jsonItemTable = jsonData.optJSONArray(TABLE_NAME)    
    for (int i = 0; i <= jsonItemTable.length(); i++) {
        JSONObject jsonItem = (JSONObject) jsonItemTable.get(i);
        Item thisItem = new Item(jsonItem);
        itemList.add(thisItem);
    }
    return itemList;
}

The for-loop is throwing a NullPointerException, which leads me to beliveve the JSONArray is not populated. here is an image of the url and the response:
URL-Results

So either the values can't be read, or the database connection fails(which I doubt, because there are no errors indicating it), but I have no idea how to verify this
Sorry for what might be a messy question. I have done any programming in almost 4 years, so a little rusty. I will happily provide any information needed.

Edit: Someone suggested a thread asking what a NullPointerException is, might hold the answer to my problem, but I know what a npe is, I just can't figure out why it's getting thrown

Update: D/HTTP GET: status 200, which should be "Ok"?
I logged the itemData String, and got this, so it can at least get the data: enter image description here Same with jsonItemsString: enter image description here Could it be the {"records": - Part which messes it up? It did not used to be there, but it seems updates to the api adds this.

SlyTech404
  • 198
  • 1
  • 11
  • I would HIGHLY recommend using a library like retrofit for API calls. It will make things so much easier. https://github.com/square/retrofit – brandonx Apr 09 '19 at 22:20
  • Can you printf jsonItemsString so we can check the json content? The problem may come from here... – Hichem BOUSSETTA Apr 09 '19 at 23:48
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Apr 10 '19 at 04:55
  • 1
    Can you debug, and check if are getting value inside jsonItemString and then with jsonData so that it would be easy to identify it! – Niki Apr 10 '19 at 13:09

1 Answers1

0

Ok, so I found out where it went wrong. All I did was swap

JSONArray jsonItemTable = jsonData.optJSONArray(TABLE_NAME);

With

JSONArray jsonItemTable = jsonData.getJSONArray("records");

In Item.java
No clue why it worked in my first version, but thank you to all who commented and got me on the right track!

SlyTech404
  • 198
  • 1
  • 11