Issue
I am stuck on the following problem and I havent been able to find a solution after a couple hours of googling and trying different solutions.
I have a JSONArray similar to the following:
[
{
"id":"3000",
"ref":"Order100",
"extraOptions": { "someOption":"1" }
},
{
"id":"3001",
"ref":"Order101",
"extraOptions": { "someOption":"1" }
},
{
"id":"3002",
"ref":"Order102",
"extraOptions": { "someOption":"1" }
}
]
If I understand this correctly then this JSONArray contains 3 JSONObjects, right?
Well, I have the following code to iterate over the JSONArray:
public void processFinish(JSONArray response){
ArrayList<String> partnerList = new ArrayList<>();
for(int i = 0; i < response.length(); i++){
try {
JSONObject rec = response.getJSONObject(i);
partnerList.add(rec.getString("ref"));
}catch (Exception e){
e.printStackTrace();
}
}
purchaseOrders = 1;
fillList(partnerList);
}
Now, for reasons I do not understand, response.length() returns 1, and when the for loop is done it only shows one entry in my list. What kind of mistake am I making here/what am I missing? Thanks in advance for any kind of help.
EDIT: The problem seems to be in my AsyncTask onPostExecute(String s) function which looks like the following.
@Override
protected void onPostExecute(String s){
super.onPostExecute(s);
if(s != null){
try {
System.out.println(s);
JSONArray resp = new JSONArray(s);
delegate.processFinish(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
I get the JSON from the API of a Web-App. This Web-App also offers the possibility to test the API on page and shows you what the Response Body looks like. So I changed String s and just copied the Response Body from the Webpage into there and with that it seems to work just fine and iterate over it correctly.
However when I use the API as intented like in the function above, it does not work.
EDIT2: Problem solved. Find the answer by me below in the answer section. Thank you to the people that tried to help me and steered me in the right direction.
Solution
The API was not returning the correct amount of JSON Objects, thats why it was only showing 1 entry in the list. I should have tested that before I asked the question. Again thank you to everyone that tried to help.
Answered By - Dokik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.