Reputation: 5246
As you can see in the picture I have JSON object 'multimedia' which has information about picture in 4 different formats. I need url only on of them. Lets say which had standard format (75x75). I use volley library in my android application. I am confused about how to take/parse url (in string format is enough) of image that underlined in the picture.
Here is code that I used:
NewsFragment:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
sendJsonRequest();
}
private void sendJsonRequest(){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
getRequestUrl(10),
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
parseJSONRequest(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
private void parseJSONRequest(JSONObject response){
if(response==null || response.length()==0){
return;
}
try {
JSONArray arrayResult = response.getJSONArray(Keys.EndPointNews.KEY_RESULTS);
for (int i = 0; i < arrayResult.length(); i++){
JSONObject currentResult = arrayResult.getJSONObject(i);
String section = currentResult.getString(Keys.EndPointNews.KEY_SECTION);
String subsection = currentResult.getString(Keys.EndPointNews.KEY_SUBSECTION);
String title = currentResult.getString(Keys.EndPointNews.KEY_TITLE);
String article_abstract = currentResult.getString(Keys.EndPointNews.KEY_ABSTRACT);
String published_date = currentResult.getString(Keys.EndPointNews.KEY_PUBLISHED_DATE);
// HERE IS A PROBLEM: EDIT:
JSONArray arrayMultimedia = currentResult.getJSONArray(Keys.EndPointNews.KEY_MULTIMEDIA);
JSONObject objectMultimedia = arrayMultimedia.getJSONObject(0);
String multimediaURL = null;
if(objectMultimedia.has(Keys.EndPointNews.KEY_MULTIMEDIA_URL))
{
multimediaURL = objectMultimedia.optString(Keys.EndPointNews.KEY_MULTIMEDIA_URL);
}
News news = new News();
news.setSection(section);
news.setSubsection(subsection);
news.setArticleTitle(title);
news.setArticleAbstract(article_abstract);
Date date = mDateFormat.parse(published_date);
news.setPublishedDate(date);
//EDIT
news.setMultimediaURL(multimediaURL);
mListNews.add(news);
}
Toast.makeText(getActivity(),mListNews.toString(),Toast.LENGTH_LONG).show();
}catch (JSONException e){
}
catch (ParseException e) {
e.printStackTrace();
}
}
THANKS FOR ANY HELP!
EDIT:
public String getMultimediaURL(){
return multimediaURL;
}
public void setMultimediaURL(String multimediaURL){
this.multimediaURL = multimediaURL;
}
Upvotes: 0
Views: 2866
Reputation: 6360
I must suggest you to go with GSON library for parsing your JSON reposnses. it is very easy, you have to just create your template/entity classes. here is the link and download gson library from here
OR
refer below answer by @ρяσѕρєя K
OR
refer this answer
Upvotes: 2
Reputation: 132982
multimedia
is JSONArray instead of JSONObject. get multimedia
json array from currentResult
JSONObject:
JSONObject currentResult = arrayResult.getJSONObject(i);
JSONArray arrMultimedia = currentResult.getJSONArray(
Keys.EndPointNews.KEY_MULTIMEDIA);
multimedia
array contain JSONObejct so get JSONObject from arrMultimedia
to get all values using key:
JSONObject jsonObjMultimedia = arrMultimedia.getJSONObject(0);
String strPicUrl=jsonObjMultimedia.optString("url");
Upvotes: 1