如何采取/解析图像的网址是什么? JSON /凌空库 [英] how to take/parse url of image? JSON / Volley library

查看:154
本文介绍了如何采取/解析图像的网址是什么? JSON /凌空库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你可以在图片中我的 JSON对象'多媒体'见有大约在4个不同格式的图像信息。我需要的网址是否只对其中。比方说,其中有标准格式(75x75)。我在Android应用程序中使用抽射库。我感到困惑的如何采取/ URL解析(字符串格式就够了),在画面强调图像

下面是的 code ,我用:

NewsFragment:

  @覆盖
    公共无效onActivityCreated(@Nullable捆绑savedInstanceState){
        super.onActivityCreated(savedInstanceState);        volleySingleton = VolleySingleton.getInstance();
        请求队列= volleySingleton.getRequestQueue();
        sendJsonRequest();
    }    私人无效sendJsonRequest(){
        JsonObjectRequest要求=新JsonObjectRequest(Request.Method.GET,
                getRequestUrl(10),
                空值,
                新Response.Listener<&JSONObject的GT;(){
                @覆盖
                公共无效onResponse(JSONObject的响应){
                    parseJSONRequest(响应);
                }
                },新Response.ErrorListener(){
                @覆盖
                公共无效onErrorResponse(VolleyError错误){                }
                });
        requestQueue.add(请求);
    }    私人无效parseJSONRequest(JSONObject的响应){
        如果(响应== NULL || response.length()== 0){
            返回;
        }
        尝试{
            JSONArray arrayResult = response.getJSONArray(Keys.EndPointNews.KEY_RESULTS);            的for(int i = 0; I< arrayResult.length();我++){
                JSONObject的currentResult = arrayResult.getJSONObject(I)                弦乐部分= currentResult.getString(Keys.EndPointNews.KEY_SECTION);
                字符串款= currentResult.getString(Keys.EndPointNews.KEY_SUBSECTION);
                字符串title = currentResult.getString(Keys.EndPointNews.KEY_TITLE);
                字符串article_abstract = currentResult.getString(Keys.EndPointNews.KEY_ABSTRACT);
                字符串published_date = currentResult.getString(Keys.EndPointNews.KEY_PUBLISHED_DATE);                //这里有一个问题:编辑:
                JSONArray arrayMultimedia = currentResult.getJSONArray(Keys.EndPointNews.KEY_MULTIMEDIA);
                的JSONObject objectMultimedia = arrayMultimedia.getJSONObject(0);
                字符串multimediaURL = NULL;
                如果(objectMultimedia.has(Keys.EndPointNews.KEY_MULTIMEDIA_URL))
                {
                       multimediaURL = objectMultimedia.optString(Keys.EndPointNews.KEY_MULTIMEDIA_URL);
                }                新闻新闻=新新闻();                news.setSection(部分);                news.setSubsection(款);
                news.setArticleTitle(职称);
                news.setArticleAbstract(article_abstract);
                日期日期= mDateFormat.parse(published_date);
                news.setPublishedDate(日期);                //编辑
                news.setMultimediaURL(multimediaURL);                mListNews.add(新闻);
            }
            Toast.makeText(getActivity(),mListNews.toString(),Toast.LENGTH_LONG).show();
        }赶上(JSONException E){        }
        赶上(ParseException的E){
            e.printStackTrace();
        }
}

任何帮助的感谢!

编辑:

 公共字符串getMultimediaURL(){
     返回multimediaURL;
}
公共无效setMultimediaURL(字符串multimediaURL){
     this.multimediaURL = multimediaURL;
}


解决方案

我要建议你GSON库去解析您的JSON reposnses。这是很容易,你只需创建模板/实体类。这里是从<一的链接和下载GSON库href=\"https://$c$c.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.4-release.zip&\"相对=nofollow>这里

下面参考答案由@ρяσѕρєя氏/ P>

回答

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;
}

解决方案

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

这篇关于如何采取/解析图像的网址是什么? JSON /凌空库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆