【问题标题】:how to take/parse url of image? JSON / Volley library如何获取/解析图像的 url? JSON / Volley 库
【发布时间】:2015-04-20 11:04:05
【问题描述】:

正如您在图片中看到的,我有 JSON 对象“多媒体”,其中包含 4 种不同格式的图片信息。我只需要其中的网址。让我们说哪个具有标准格式(75x75)。我在我的 android 应用程序中使用 volley library。我对如何获取/解析图片中带下划线的图像的 url(字符串格式就足够了)感到困惑。

这是我使用的代码

新闻片段:

    @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();
        }
}

感谢您的帮助!

编辑:

public String getMultimediaURL(){
     return multimediaURL;
}
public void setMultimediaURL(String multimediaURL){
     this.multimediaURL = multimediaURL;
}

【问题讨论】:

  • 使用 GSON 解析数据

标签: android json android-fragments android-volley android-json


【解决方案1】:

我必须建议您使用 GSON 库来解析您的 JSON 响应。这很容易,您只需创建模板/实体类。这是link 并从here 下载gson 库

请参考@ρяσѕρєя K 的以下回答

参考this答案

【讨论】:

  • 感谢您的信息,我只是迈出了第一步,需要学习它。 =)
【解决方案2】:

multimedia 是 JSONArray 而不是 JSONObject。从currentResult JSONObject 中获取multimedia json 数组:

JSONObject currentResult = arrayResult.getJSONObject(i);
JSONArray arrMultimedia = currentResult.getJSONArray(
                                     Keys.EndPointNews.KEY_MULTIMEDIA);

multimedia 数组包含 JSONObejct,所以从 arrMultimedia 获取 JSONObject 以使用键获取所有值:

JSONObject jsonObjMultimedia = arrMultimedia.getJSONObject(0);
String strPicUrl=jsonObjMultimedia.optString("url");

【讨论】:

  • 好的,谢谢,我会尝试将它用作 JSONArray 但无论如何我只需要一个具有格式 (75x75) 的图片 url。你有什么建议?
  • 您好!你能再看看我的帖子吗?我用你的代码更新了它,但不幸的是没有结果。现在怎么了?
  • @NurzhanNogerbek:使用String strPicUrl=jsonObjMultimedia.optString("url");行的日志并检查是否在logcat中获取值
  • 我使用了 Log.i(TAG, "multimediaURL value:" + mediaURL);我在 logcat 中有价值,但为什么它没有在 Toast 消息中显示。我对此感到困惑。
  • @NurzhanNogerbek:尝试只显示图片网址,例如Toast.makeText(getActivity(),mListNews.get(0).getMultimediaURL().get.toString(),Toast.LENGTH_LONG).show();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
相关资源
最近更新 更多