简体   繁体   中英

How to show other Json objects in RecylerView on Android

I want develop android application for one website. I read website posts from json and show its in RecyclerView every 10 posts.
I can show title, description and thumbnail. but i want show medium from thumbnail_images instance of thumbnail . I don't know how to read images from medium ?!

My Json Link : Link

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");
                        //get other data
                        JSONObject imageObj = postObject.getJSONObject("thumbnail_images");
                        JSONObject mediumObj = imageObj.optJSONObject("medium");
                        String mediumImage = mediumObj.getString("url");
                            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(mediumImage)));

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

for fetch medium image i use this code :

                            //get other data
                            JSONObject imageObj = postObject.getJSONObject("thumbnail_images");
                            JSONObject mediumObj = imageObj.optJSONObject("medium");
                            String mediumImage = mediumObj.getString("url");

but when set mediumImage for infoModels.add(new MainDataModel() not show me any posts!

How can set images from medium ? thanks all <3

   private void setImageWithPicaso(String imageUrl) {
    if (!(imageUrl == null)) {
        Picasso.with(getActivity()).load(imageUrl).placeholder(R.drawable.placeholder_background).into(imageView, new Callback() {
            @Override
            public void onSuccess() {
                //On Success
            }

            @Override
            public void onError() {
                spinner.setVisibility(View.GONE);
                //On Error


            }
        });
    } else {
        spinner.setVisibility(View.GONE);
       //On Error
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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