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:
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:
Same with jsonItemsString:
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.