【问题标题】:Get all Instagram feeds using londatiga library使用 londatiga 库获取所有 Instagram 提要
【发布时间】:2018-02-26 17:47:36
【问题描述】:

我目前正在开发一个社交媒体应用程序,我想获取所有 Instagram 图片提要。我正在使用 londatiga 库,到目前为止,我只获得了 10 个提要,我想获得所有提要,我使用的代码是:

public class DownloadTask extends AsyncTask<URL, Integer, Long> {

    protected void onCancelled() {
    }

    protected void onPreExecute() {
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Feeds...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Long doInBackground(URL... urls) {
        long result = 0;

        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>(1);

            params.add(new BasicNameValuePair("count", "10"));

            InstagramRequest request = new InstagramRequest(
                    mInstagramSession.getAccessToken());
            String response = request.createRequest("GET",
                    "/users/self/feed", params);

            if (!response.equals("")) {
                JSONObject jsonObj = (JSONObject) new JSONTokener(response)
                        .nextValue();
                JSONArray jsonData = jsonObj.getJSONArray("data");

                int length = jsonData.length();
                flag_loading = false;

                if (length > 0) {

                    for (int i = 0; i < length; i++) {
                        JSONObject jsonPhoto = jsonData.getJSONObject(i)
                                .getJSONObject("images")
                                .getJSONObject("low_resolution");
                        mPhotoList.add(jsonPhoto.getString("url"));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(Long result) {
        pDialog.dismiss();
        adapter.notifyDataSetChanged();
    }
}

【问题讨论】:

    标签: android instagram


    【解决方案1】:
        private LinkedList<String> mPhotoList = new LinkedList<String>();
        private LinkedList<String> mPhotoListIds = new LinkedList<String>();
    
        private static String nextMaxId = "";
        private static String url = "";
    
        //onCreateView
    
    
            listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
    
                }
    
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
                        if (flag_loading == false) {
                            flag_loading = true;
                            new DownloadTask(true).execute();
                        }
                    }
                }
            });
            if (mInstagramSession.isActive()) {
                new DownloadTask(false).execute();
            }
    
    
    
    
    
        ProgressDialog pDialog;
    
        public class DownloadTask extends AsyncTask<URL, Integer, Long> {
    
            private boolean isBeingPaginated = false;
    
            public DownloadTask(boolean isBeingPaginated) {
                this.isBeingPaginated = isBeingPaginated;
            }
    
            protected void onCancelled() {
    
            }
    
            protected void onPreExecute() {
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Getting Feeds...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
    
            protected Long doInBackground(URL... urls) {
                long result = 0;
    
    
                try {
                    List<NameValuePair> params = new ArrayList<>(1);
                    params.add(new BasicNameValuePair("count", "10"));
    
    
                    if (isBeingPaginated) {
                        url = "https://api.instagram.com/v1/tags/self/media/recent?access_token=" + mInstagramSession.getAccessToken() + "&max_id=" + nextMaxId;
    
                        params.add(new BasicNameValuePair("next_url", url));
                    }
    
                    InstagramRequest request = new InstagramRequest(
                            mInstagramSession.getAccessToken());
                    String response = request.createRequest("GET",
                            "/users/self/feed", params);
    
                    if (!response.equals("")) {
                        JSONObject jsonObj = (JSONObject) new JSONTokener(response)
                                .nextValue();
                        JSONArray jsonData = jsonObj.getJSONArray("data");
                        JSONObject jsonPaginatioNData = jsonObj.getJSONObject("pagination");
    
                        //Fetching nextMaxId
                        if (jsonPaginatioNData.length() > 0) {
                            //JSONObject paginationJsonObject = jsonPaginatioNData.getJSONObject(0);
                            nextMaxId = jsonPaginatioNData.getString("next_max_id");
                            Log.d("max", nextMaxId);
                            isBeingPaginated = true;
    
                        }
    
                        int length = jsonData.length();
                        flag_loading = false;
    
                        if (length > 0) {
    
                            for (int i = 0; i < length; i++) {
                                JSONObject jsonPhoto = jsonData.getJSONObject(i)
                                        .getJSONObject("images")
                                        .getJSONObject("low_resolution");
                                JSONObject jsonUser = jsonData.getJSONObject(i)
                                        .getJSONObject("users_in_photo")
                                        .getJSONObject("user");
    
                                String jsonPhotoIds = jsonData.getJSONObject(i)
                                        .getString("id");
                                String jsonPhotoIds = jsonData.getJSONObject(i)
                                        .getString("id");
                                Log.d("ID: ", jsonPhotoIds);
    
                                mPhotoList.addjsonPhoto.getString("url")
                                mPhotoListIds.add(jsonPhotoIds);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                return result;
            }
    
            protected void onProgressUpdate(Integer... progress) {
            }
    
            protected void onPostExecute(Long result) {
                pDialog.dismiss();
                adapter.notifyDataSetChanged();
            }
        }
    
    }
    

    这就是我所做的,现在我在 Listview 滚动上获得了新的提要

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多