How to show multiple images using retrofit android - android

Hello below is my image ulrs thats comes from backend services. The images are separated with comma(,). Please let me know how can i separate each images and set them to imageView.
Below is the url
{"data":[{"title":"Manali","organizer":"SINGH","image":",
Img_5c3ff5642013e20190117_084929.jpg,
Img_5c3ff564204b720190117_085027.jpg,
Img_5c3ff564206abPhoto_1547695381805.png,
Img_5c3ff56420e2f20190117_084853.jpg,",
"date1":"21 Apr 2019",
"date2":"24 Jan 2019",
"time1":"10:30AM",
"time2":"2:00PM",
"address":"VASANT VIHAR D-BLOCK MARKET NEW DELHI",
"city":"Delhi",
"state":"Delhi",
"price":"7000"}
]
}
Thanks in advance

TextUtils.split(",", fullImageString); will give you the images in a string array, and depending on how to use them you can show the images fetching the values from what you created. It is not possible to show multiple images in one ImageView however you can show multiple images with GridView, ListView or RecyclerView that holds multiple ImageView references, with using an adapter and such. If these are image URLs, use this library to efficiently process each URL and display them on image views, while working with list adapters.

To extract the urls, you would do the following :
try {
JSONObject jsonObject = new JSONObject(resultFromWebservice);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject innerObj = jsonArray.getJSONObject(i);
String imgURLs = innerObj.getString("image");
ArrayList urlList= new ArrayList(Arrays.asList(imgURLs.split(",")));
for (int j = 0; j < urlList.size(); j++) {
Log.d("Extracted URLs", "myMethod: " + urlList.get(j));
// You can set the imageView urls from here
}
}
} catch (JSONException e) {
e.printStackTrace();
}

Related

How to fetch Json array data in android studio using volley?

I have a JSON encode array given bellow:-
{
"imager": [{
"title": "Guru",
"images": ["images\/6.png", "images\/androidIntro.png", "images\/barretr_Earth.png"]
}]
}
My Problem is that I want to fetch all the images from images array one by one so that I can show the images on imageview. my Main aim is to display Title only one time and show all the images related to the title, I have searched all over the internet and stackflow but I am unable to find the proper answer
could anyone help me solving this?
I am using Volley Libabry here is my code:-
url = "myurl";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("imager");
for (int i=0; i<jsonArray.length(); i++)
{
JSONObject object = jsonArray.getJSONObject(i);
String title = object.getString("title");
String images = object.getString("images");
// what to do here now?? help me?
}
if I set
textview.setText(images);
output will be like this:-
["images\/6.png","images\/androidIntro.png","images\/barretr_Earth.png"]
but i want only
images/6.png
images/androidIntro.png
images/battetr_Earth.png
So that I can so these all images in imageview.
You have to handle the "images" tag as an array.
Instead of
String images = object.getString("images");
use
JsonArray images = object.getJSONArray("images");
for (int j=0; j<images.length(); j++) {
String image = images.getString(j)
// image will be
// j = 0 -> "images\/6.png"
// j = 1 -> "images\/androidIntro.png"
// j = 2 -> "images\/barretr_Earth.png"
}

How can i parse this json file in android studio? [duplicate]

This question already has answers here:
Android Parse JSON Array
(3 answers)
Closed 6 years ago.
how can i parse this json file because the text do not have any particular indexing names
{
"titles": [
" Thoughts On The Works Of Providence",
"\"All Is Vanity, Saith the Preacher\"",
"\"And the sins of the fathers shall be\"",
"\"Arcturus\" is his other name",
"\"By the Waters of Babylon.\"",
"\"De Gustibus--\"",
"\"Faith\" is a fine invention",
"\"Faithful to the end\" Amended",
"\"Heaven\" -- is what I cannot reach!",
"\"Heaven\" has different Signs -- to me --",
"\"Heavenly Father\" -- take to thee",
"\"Home\"",
]
}
Like this:
JSONArray array = object.getJSONArray("titles");
for(int i = 0; i < array.length(); i ++){
String title = array.getString(i);
//Do something with the string
}
You treat the JsonArray like a string array.
However in the array you provided the JSON is invalid because of the comma on the last element of the array, you need to remove this comma to have valid JSON.
It's just a simple JSON. Do it like this.
String json = "{\"titles\":[\" Thoughts On The Works Of Providence\",\"\\\"All Is Vanity, Saith the Preacher\\\"\",\"\\\"And the sins of the fathers shall be\\\"\",\"\\\"Arcturus\\\" is his other name\",\"\\\"By the Waters of Babylon.\\\"\",\"\\\"De Gustibus--\\\"\",\"\\\"Faith\\\" is a fine invention\",\"\\\"Faithful to the end\\\" Amended\",\"\\\"Heaven\\\" -- is what I cannot reach!\",\"\\\"Heaven\\\" has different Signs -- to me --\",\"\\\"Heavenly Father\\\" -- take to thee\",\"\\\"Home\\\"\"]}";
JSONObject jsonObject = new JSONObject(json);
And for accessing your items:
JSONArray jsonArray = jsonObject.getJSONArray("titles");
for(int i=0;i<jsonArray.size(); i++){
Log.d(TAG, jsonArray.getString(i));
}
You can parse this to an instanse of this class :
public class Instance{
public String[] titles;
}
but you need to remove the last comma after home. If there is no another item after the comma, it means it is not a valid JSON string.
This should work.
JSONObject jsonObject = new JSONObject(yourString);
JSONArray jsonArray= jsonObject.getJSONArray("titles");
for (int i=0; i < jsonArray.length(); i++) {
string content = jsonArray.getString(i);
....
}
Try this:
Try{
JSONArray itemArray = jsonObject.getJSONArray("titles")
for (int i = 0; i < itemArray.length(); i++) {
String value=itemArray.getString(i);
Log.e("json", i+"="+value);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Display images dynamically into list view items

i develop android apps since one year so i'm not able to solve this kind of problem. I searched many times on our friend google but 0 real result. This is a very precise question, i try to display images dynamically into listview items, i mean :
1- I receive an array of int from my database (ex : 5, 6, 7, 7)
2- I want the adpater to display differents images depending of this numbers
for exemple i receive : "result" = {"1", "2", "3"} i want the app to associate images to this numbers (Images come from drawable folder)
if (int number = 1) {
imageview into item layout.setImageRessource(R.id.blabla)
}else ...
I really don't know how do that, i tried building a custom adapter but it doesn't display the listview...
I'll be the happiest developper if somebody can tell me what the good way to do that.
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON2);
Poeple2 = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < Poeple2.length(); i++) {
JSONObject c = Poeple2.getJSONObject(i);
String username = c.getString(TAG_USERNAME);
int mood = c.getInt(TAG_MOOD);
HashMap<String, String> persons = new HashMap<String, String>();
persons.put(TAG_USERNAME, username);
persons.put(TAG_MOOD, String.valueOf(mood));
personList2.add(persons);
}
// i used a simple adapter but i know it's a wrong way
adapter = new SimpleAdapter(getActivity(), personList2, R.layout.modeluser4, new String[]{TAG_USERNAME, TAG_MOOD}, new int[]{R.id.tvHypo2, R.id.tvId});
list3.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
create a switch like
public void runTheSwitch(int num){
int number = num;
switch(number){
case 1:
//add image to listview
case 2:
//add image to listview
case 3:
//add image to listview
....and so on...
}
}
When you recieve the Array of numbers from database (lets call it ArrayNum), run a loop through those num.
For(int num : ArrayNum){
runTheSwitch(num);
}
Put this into a method and run the method before you set your adapter. So basically in this method you add items to your Arraylist like arraylist.add(); then after this you define an Object of your custom adapter and pass the Arraylist in your adapter.
Hope it helps

Android Load Image From URL Into ListView

I have a ListView onto which I would like to load image from url where images names are an ID.jpg (eg: 1.jpg / 2.jpg / 3.jpg.. etc). The IDs are from the database, however the images loaded into my ListView for all rows is one image (1.jpg).
txtDate.setText(Request_Date.get(position));
txtTime.setText(Request_Time.get(position));
//ivImage.setImageResource(imgid[position]); <-- this works from drawable and not from database
Picasso.with(context).load(url_poster + request_eventID + ".jpg").into(ivImage);
The request_eventID are IDs from DB, this is my problem, it only returns 1 (and not the rest of the IDs) so therefore only 1.jpg is loaded into the ListView on all rows.
EDIT:
How I retrieve IDs
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("read_columns");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String request_date = jsonChildNode.optString("Date");
String request_time = jsonChildNode.optString("Time");
request_eventID = jsonChildNode.optString("EID");
arrRequest_Date.add(request_date);
arrRequest_Time.add(request_time);
arrRequest_EID.add(request_eventID);
Note that I can see all IDs if I do a System.out.println("IDs" +request_eventID)
Ok, just as I suspected, I played around with the code a bit and indeed get(position) was what I needed on the IDs.
Solution:
Picasso.with(context).load(url_poster + Request_EID.get(position) + ".jpg").into(ivImage);
Thank you all for responding.

How to fetch Data From JSON

I have following data from JSON
{
"MenuName": "starter dish",
"SubMenu": [
"pizza dish1",
"pizza dish2"
],
"Price": [
"100",
"110"
]
},
From here i can easily fetch data from "Menuname" key as starter dish but when I fetch data from "Submenu" I get whole string as ["pizza dish1", "pizza dish2"].
Please suggest me way to differentiate both pizza dish1 and pizza dish2
Submenu and Price is not String it is JSONArray so you will need to use for-loop to get all values from Submenu JSONArray as:
JSONArray jsonsubmenu=yourjsonobject.getJSONArray("Submenu");
for(int i=0;i < jsonsubmenu.length();i++){
// get all values from jsonsubmenu JSONArray..
String str_value=jsonsubmenu.optString(i);
....
}
try this
for (int i = 0; i < jsonArray.length(); i++) {
jsonArray.getJSONObject(i).getInt("SubMenu");
}
you can use this link for creating POGO class for your response. It will automatically generate class for your response.
Use google GSON library to parse your response.
or you can simply create a JSON Array or Objects to parse it. in your case it is JSON Object or JSON arrays.
Hi There in that senarerio you have to use
JSONArray _submenu = object.getJSONArray("SubMenu");
for (int i = 0; i < _submenu.length(); i++) {
String text = _submenu.getString(i);
}
JSONArray _price = object.getJSONArray("Price");
for (int i = 0; i < _price.length(); i++) {
String text = _price.getString(i);
}
You can retrieve array values and store it in string[] and use them as per your need. i.e., as follows..
try {
JSONObject jObject = new JSONObject(jsonString);
JSONArray jSubmenu = jObject.getJSONArray("SubMenu");
subMenu = new String[jSubmenu.length()];
for (int i = 0; i < subMenu.length; i++) {
subMenu[i] = jSubmenu.getString(i);
}
JSONArray jPrice = jObject.getJSONArray("Price");
price = new String[jPrice.length()];
for (int i = 0; i < price.length; i++) {
price[i] = jPrice.getString(i);
}
} catch (Exception e) {
// TODO: handle exception
}
Just to throw in a quickie - read up on, and use GSON.
For simple small jobs I find it is the best. Not the fastest running for complex, or long structures, but really quick on the dev side.
Here's the link: google-gson

Categories

Resources