Java 如何连接到 TMDB api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19439183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:09:34  来源:igfitidea点击:

How to connect to TMDB api

javaapithemoviedb-api

提问by Jusleong

Here is my api key: 7b5e30851a9285340e78c201c4e4ab99

这是我的 api 密钥:7b5e30851a9285340e78c201c4e4ab99

And I am trying to connect to TMDB api: here is my code:

我正在尝试连接到 TMDB api:这是我的代码:

package movieDBapiconnnection;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class connection {

    public static void main(String[] args) throws Exception{
            URL url = new URL("http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json");

            BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
    }
}

But it always showing me the error that:

但它总是向我展示以下错误:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at movieDBapiconnnection.connection.main(connection.java:17)

线程“main”中的异常 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 401:http: //api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550at sun。 net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at movieDBapiconnnection.connection.main(connection.java:17)

回答by Mike Marks

First, I would use a TMDB wrapper. Get a Java wrapper at: https://github.com/Omertron/api-themoviedb. Use the fully tested and tried wrapper rather than trying to connect to it and creating your models from scratch. Typically in a wrapper when you instantiate a class, you pass in the API key into the constructor and the wrapper does the rest.

首先,我将使用 TMDB 包装器。在以下位置获取 Java 包装器:https: //github.com/Omertron/api-themoviedb。使用经过充分测试和试用的包装器,而不是尝试连接到它并从头开始创建模型。通常在实例化类时在包装器中,您将 API 密钥传递到构造函数中,包装器会完成剩下的工作。

回答by fedmich

Your error is simply because of extra characters in your API key parameter

您的错误仅仅是因为您的 API 密钥参数中有多余的字符

http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550

http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550

remove the /3/movie/550, as this is just a typo, so the correct one is

删除/3/movie/550,因为这只是一个错字,所以正确的是

http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99

http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99

Note: Change 7b5e30851a9285340e78c201c4e4ab99into your correct API key

注意:将7b5e30851a9285340e78c201c4e4ab99更改为正确的 API 密钥

回答by Chandan Sharma

You passed the wrong URL, pass this URL "http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99

你传递了错误的 URL,传递这个 URL " http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99

URL url = new URL("http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99");

回答by Yuba Ait Arrami

try to the the networking part in a background thread. using main thread for this kind of operation will likely give you a similar error. use an AsynchTask and doInBackground() to achieve this.

尝试在后台线程中访问网络部分。使用主线程进行此类操作可能会给您带来类似的错误。使用 AsynchTask 和 doInBackground() 来实现这一点。