0
I have a json on a server with image links and need to convert to imagemView, I could only open on the android screen as string showing the image link.
my code that receives json
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String string) {
parseJsonData(string);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getApplicationContext(), "Some error occurred!!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(request);
}
void parseJsonData(String jsonString) {
try {
JSONObject object = new JSONObject(jsonString);
JSONArray fruitsArray = object.getJSONArray("fruits");
ArrayList al = new ArrayList();
for(int i = 0; i < fruitsArray.length(); ++i) {
al.add(fruitsArray.getString(i));
}
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, al);
fruitsList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
dialog.dismiss();
}
on the server side, convert the images to Base64 string. When receive on android moves from Base64 to image again
– AndersonCanteiro
@How to convert Array? I had not yet thought about Base64 I will try, but I do not know how to pick up the Array that will come and play for picture.
– Marlos Trinidad
you will need to make your own Adapter, look at this tutorial http://blog.alura.com.br/personalizando-uma-listview-no-android/, the part that is passed the java drawable, you make a function that takes the json Base64 and turns into image
– AndersonCanteiro
@Andersoncanteiro in this case I will receive in string is not? if that is I am already receiving in string the url of the images, and I transformed an image using Base64 became a very big code, has an example?
– Marlos Trinidad
I’m getting the link of each image on android in string array, I’m just not knowing how to pick up the array string and run
– Marlos Trinidad
Base64 gets big, but then on android you only have to convert, in case you send the url, vc will have to make another request to the internet. That may be the case, too, but the way I put it, you’d only request
– AndersonCanteiro
@Andersoncanteiro you have an example? because I am beginner, about json and this subject I am new, I would be grateful, thank you
– Marlos Trinidad
No need to convert to Base64. Libraries like Glide and Picasso can load the images from the urls directly into a
ImageView
.– Leonardo Lima
Marlos, I’m gonna see if I can put something together, you really don’t have to convert, I told you to convert just to make a request. But if that’s okay, you can use Leonardo’s suggestion.
– AndersonCanteiro