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":[]}]}
I'm not sure what Json library you're using, but it looks like org.json
. I thought your code looked sane, so I implemented it and do not see the output that you are seeing. My guess is that your input data isn't what you expect it to be.
final JSONArray menuObj = new JSONArray("[\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"item\": \"tea\",\n" +
" \"price\": \"7.5\",\n" +
" \"image\": \"http://192.168.1.3/CI/images/tea.jpg\",\n" +
" \"veg\": \"0\",\n" +
" \"category\": \"drinks\"\n" +
" },\n" +
" {\n" +
" \"id\": \"2\",\n" +
" \"item\": \"coffee\",\n" +
" \"price\": \"10\",\n" +
" \"image\": \"http://192.168.1.3/CI/images/coffee.jpg\",\n" +
" \"veg\": \"0\",\n" +
" \"category\": \"drinks\"\n" +
" }\n" +
"]");
for (int i = 0; i < menuObj.length(); i++){
final JSONObject row = menuObj.getJSONObject(i);
System.out.println("imageUrl: " + row.getString("image"));
System.out.println("rowString: " + row);
}
Output:
imageUrl: http://192.168.1.3/CI/images/tea.jpg rowString: {"image":" http://192.168.1.3/CI/images/tea.jpg ","item":"tea","price":"7.5","veg":"0","id":"1","category":"drinks"} imageUrl: http://192.168.1.3/CI/images/coffee.jpg rowString: {"image":" http://192.168.1.3/CI/images/coffee.jpg ","item":"coffee","price":"10","veg":"0","id":"2","category":"drinks"}
To parse JSON Array :
JSONArray jArray = jObject.getJSONArray("menu");
for (int i = 0; i < jArray.length(); i++) {
JSONObject innerjObject =jArray.getJSONObject(i);
String image =innerjObject.getString("image");
array_list.add(image);
}
output: http://192.168.1.3/CI/images/tea.jpg
And to display this image to imageview use library :
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
using Loadimagview method of library load image in imageview
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.