From a Rest API, I get json data in the following format:
[
{
"id": "1",
"item": "tea",
"price": "7.5",
"image": "http:\/\/192.168.1.3\/CI\/images\/tea.jpg",
"veg": "0",
"category": "drinks"
},
{
"id": "2",
"item": "coffee",
"price": "10",
"image": "http:\/\/192.168.1.3\/CI\/images\/coffee.jpg",
"veg": "0",
"category": "drinks"
}
]
From the API I get Json as a string and it contains backslashes in front of url's forward slashes, which is according to the json encoding specification. And I am correctly able to json_decode and get url from php. In android I store the json string in a variable named "menu_json".
Then I am trying to parse and get the image url from it using the following code:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
try{
JSONObject menuApiObj = new JSONObject(menu_json);
JSONArray menuObj = menuApiObj.getJSONArray("menu");
for (int i = 0; i < menuObj.length(); i++){
JSONObject row = menuObj.getJSONObject(i);
rowString = row.getString("image");
imageUrl = row.toString();
Log.e("rowString", rowString);
Log.e("imageUrl", imageUrl);
}
The output I get is:
{
"id": "1",
"item": "tea",
"price": "7.5",
"image": "tea.jpg",
"veg": "0",
"category": "drinks"
}
The image field is supposed to be:
http://192.168.1.3/CI/images/tea.jpg
But instead I get just:
tea.jpg
When json_decode the API response in PHP, I get the correctly decoded url. But in Android, I am not getting the correctly decoded url in image field.
Please help!
Here is the complete API response:
{"menu":[{"id":"1","item":"tea","price":"7.5","image":"tea.jpg","veg":"0","category":"drinks"},{"id":"2","item":"cofee","price":"10","image":"coffee.jpg","veg":"0","category":"drinks"},{"id":"3","item":"crispy chicken","price":"160","image":"crispy-chicken.jpg","veg":"0","category":"curries"}],"cat_wise":[{"category":"drinks","items":[{"id":"1","item":"tea","price":"7.5","image":"http:\/\/192.168.1.3\/CI\/images\/tea.jpg","veg":"0","category":"drinks"},{"id":"2","item":"cofee","price":"10","image":"http:\/\/192.168.1.3\/CI\/images\/coffee.jpg","veg":"0","category":"drinks"}]},{"category":"curries","items":[{"id":"3","item":"crispy chicken","price":"160","image":"http:\/\/192.168.1.3\/CI\/images\/crispy-chicken.jpg","veg":"0","category":"curries"}]},{"category":"main","items":[]}]}
menuApiObj.getJSONArray("menu")mean? There is no such array in the given JSON data,