下一页令牌无法正常工作,只显示最多(50)个结果。

huangapple go评论68阅读模式
英文:

Next Page Token is not functional and display only maximum (50) results

问题

displayVideos();

    }


    private void displayVideos ()
    {
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {



            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    System.out.println(jsonObject.get("nextPageToken"));
                    JSONArray jsonArray = jsonObject.getJSONArray("items");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        if (jsonObject1.has("id")){
                            JSONObject jsonVideoId=jsonObject1.getJSONObject("id");
                            if (jsonVideoId.has("kind")){
                                if(jsonVideoId.getString("kind").equals("youtube#video")){
                                    JSONObject jsonObjectSnippet = jsonObject1.getJSONObject("snippet");
                                    JSONObject jsonObjectDefault=jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");

                                    String video_id=jsonVideoId.getString("videoId");

                                    VideoDetails vd=new VideoDetails();

                                    vd.setVideoId(video_id);
                                    vd.setTitle(jsonObjectSnippet.getString("title"));
                                    vd.setDescription(jsonObjectSnippet.getString("description"));
                                    vd.setUrl(jsonObjectDefault.getString("url"));

                                    videoDetailsoArrayList.add(vd);

                                }
                                //  recyclerView.setAdapter(adapter);
                                // adapter.notifyDataSetChanged();
                            }
                        }
                    }
                }catch (JSONException e) {
                    e.printStackTrace();
                }

the url I am trying to parse is

    String url="https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCVMWWQ985A_-SESZUy_SsVQ&maxResults=50&pageToken="+nextPageToken+"&order=date&pageToken=CAUQAA&key=API_KEY";

I have been searching to apply nextpage token or page token in android studio but couldn't get a specific tutorial. There are many examples, but being naive in Android Studio, I can't implement it into my code.


<details>
<summary>英文:</summary>

I am trying to fetch all videos of a channel using youtube data api, but my code is giving error and doesn&#39;t respond to PAGE token

displayVideos();

}


private void displayVideos ()
{
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener&lt;String&gt;() {



        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                System.out.println(jsonObject.get(&quot;nextPageToken&quot;));
                JSONArray jsonArray = jsonObject.getJSONArray(&quot;items&quot;);
                for (int i = 0; i &lt; jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    if (jsonObject1.has(&quot;id&quot;)){
                        JSONObject jsonVideoId=jsonObject1.getJSONObject(&quot;id&quot;);
                        if (jsonVideoId.has(&quot;kind&quot;)){
                            if(jsonVideoId.getString(&quot;kind&quot;).equals(&quot;youtube#video&quot;)){
                                JSONObject jsonObjectSnippet = jsonObject1.getJSONObject(&quot;snippet&quot;);
                                JSONObject jsonObjectDefault=jsonObjectSnippet.getJSONObject(&quot;thumbnails&quot;).getJSONObject(&quot;medium&quot;);

                                String video_id=jsonVideoId.getString(&quot;videoId&quot;);

                                VideoDetails vd=new VideoDetails();

                                vd.setVideoId(video_id);
                                vd.setTitle(jsonObjectSnippet.getString(&quot;title&quot;));
                                vd.setDescription(jsonObjectSnippet.getString(&quot;description&quot;));
                                vd.setUrl(jsonObjectDefault.getString(&quot;url&quot;));

                                videoDetailsoArrayList.add(vd);

                            }
                            //  recyclerView.setAdapter(adapter);
                            // adapter.notifyDataSetChanged();
                        }
                    }
                }
            }catch (JSONException e) {
                e.printStackTrace();
            }
the url I am trying to parse is
String url=&quot;https://www.googleapis.com/youtube/v3/search?part=snippet&amp;channelId=UCVMWWQ985A_-SESZUy_SsVQ&amp;maxResults=50&amp;pageToken=&quot;+nextPageToken+&quot;&amp;order=date&amp;pageToken=CAUQAA&amp;key=API_KEY&quot;;

I have been searchingb to apply nextpage token or page token in android studio but couldnt get specific tutorial. there are many examples but being naive in android studio I cant implement it into my code.



</details>


# 答案1
**得分**: 1

请注意,您的URL中包含两个`pageToken`参数的实例:

```html
"https://www.googleapis.com/youtube/v3/search?part=snippet&amp;channelId=UCVMWWQ985A_-SESZUy_SsVQ&amp;maxResults=50&amp;pageToken=" + nextPageToken + "&amp;order=date&amp;pageToken=CAUQAA&amp;key=API_KEY"

它应该只有一个实例才能正常工作;更精确地说,您的URL应该只包含这个实例:pageToken=" + nextPageToken + "


另一方面,您上面的代码没有显示实现分页的循环部分。也就是说,您没有展示出实际上为变量nextPageToken赋有效值的代码片段。

因此,我无法确定您的程序是否能正常工作。


分页循环应该如下所示:

// 最初没有pageToken
nextPageToken = null;
// URL如上所示,不包含pageToken参数
url = ...

do {
  调用API并使用URLurl + (nextPageToken != null ? "&pageToken=" + nextPageToken : "")
  nextPageToken = jsonObject.get("nextPageToken");
} while (nextPageToken != null)
英文:

Please note that your URL does contain two instances of the parameter pageToken:

&quot;https://www.googleapis.com/youtube/v3/search?part=snippet&amp;channelId=UCVMWWQ985A_-SESZUy_SsVQ&amp;maxResults=50&amp;pageToken=&quot;+nextPageToken+&quot;&amp;order=date&amp;pageToken=CAUQAA&amp;key=API_KEY&quot;.

It should have only one for that to work OK; to be more precise, your URL should contain only this instance: pageToken=&quot;+nextPageToken+&quot;.


On the other hand, your code above does not show the loop implementing pagination. That is that you haven't shown the piece of code where you actually assign a valid value to the variable nextPageToken.

Therefore I cannot tell if your program will work or not.


The pagination loop would look like this:

// initially no pageToken
nextPageToken = null;
// URL as above, without the parameter pageToken
url = ...

do {
  invoke the API on the URL: url + (nextPageToken != null ? &quot;&amp;pageToken=&quot; + nextPageToken : &quot;&quot;)
  nextPageToken = jsonObject.get(&quot;nextPageToken&quot;);
} while (nextPageToken != null)

huangapple
  • 本文由 发表于 2020年9月15日 01:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63889108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定