繁体   English   中英

如何在Android中显示JSON中的其他对象

[英]How to show other objects from json in Android

我想为一个网站开发android应用程序。 我从json阅读网站帖子,并每10个帖子在RecyclerView显示一次。
我可以显示标题,描述和缩略图。 但我想从缩略图 thumbnail_images实例显示介质 我不知道如何从媒体中读取图像?

我的Json链接: 链接

AsyncTaskCodes:

public class MainDataInfo {
    private Context mContext;
    private String ServerAddress = ServerIP.getIP();

    public void getMainDataInfo(Context context) {
        mContext = context;
        new getInfo().execute(ServerAddress + "page=1");
    }

    private class getInfo extends AsyncTask<String, Void, String> {
        EventBus bus = EventBus.getDefault();
        private String ou_response;
        private List<MainDataModel> infoModels;

        @Override
        protected void onPreExecute() {
            CustomProcessDialog.createAndShow(mContext);
            infoModels = new ArrayList<>();
        }

        @Override
        protected String doInBackground(String... params) {
            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url(ServerAddress + "page=1")
                    .build();

            Response response;
            try {
                response = client.newCall(request).execute();
                ou_response = response.body().string();
                response.body().close();
                if (ou_response != null) {
                    try {
                        JSONObject postObj = new JSONObject(ou_response);
                        JSONArray postsArray = postObj.getJSONArray("posts");
                        infoModels = new ArrayList<>();

                        for (int i = 0; i <= infoModels.size(); i++) {
                            JSONObject postObject = (JSONObject) postsArray.get(i);
                            int id = postObject.getInt("id");
                            String title = postObject.getString("title");
                            Log.d("Data", "Post id: " + id);
                            Log.d("Data", "Post title: " + title);

                            //Use the title and id as per your requirement
                                infoModels.add(new MainDataModel(
                                        postObject.getInt("id"),
                                        postObject.getString("title"),
                                        postObject.getString("content"),
                                        postObject.getString("thumbnail")));

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ou_response;
        }

        @Override
        protected void onPostExecute(String result) {
            CustomProcessDialog.dissmis();
            if (result != null) {
                bus.post(infoModels);
            }
        }
    }
}

如何设置从媒体的图片? 谢谢所有<3

try {
        JSONObject postObj = new JSONObject(ou_response);
        JSONArray postsArray = postObj.getJSONArray("posts");
        for (int i= 0; i < postsArray.length(); i++){
            JSONObject postObject = postsArray.getJSONObject(i);
            int id = postObject.getInt("id");
            String title = postObject.getString("title");
            //get other data
            JSONObject imageObj = postObject.getJSONObject("thumbnail_images");
            JSONObject mediumObj = imageObj.getJSONObject("medium");
            String mediumImage = mediumObj.getString("url");
            Log.d("Data", "id: " + id);
            Log.d("Data", "title: " + title);
            //log other data
            Log.d("Data", "the mediumObj url: " + mediumImage);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM