Java: Android: Retrofit - 使用 Call but, Response{code = 401,message=unauthorized} [英] Java: Android: Retrofit - using Call but, Response{code = 401,message=unauthorized}
问题描述
尝试使用 Retrofit 访问内容,使用电影数据库 API,但我崩溃了,没有抛出任何异常或错误消息...我是 Retrofit 的新手,但我搜索了一些文档,这就是我有(我正在使用 Retrofit 2.0):
trying to use Retrofit to access stuff, with themoviedatabase API, but i'm getting a crash, without any thrown exception or error message... I'm new to Retrofit, but i searched some documentation, and this is what i have(i'm using the Retrofit 2.0):
String movieToSearch = "fight";
String ENDPOINT = "https://api.themoviedb.org/3";
String API_KEY = "&api_key=------------------------------";
Retrofit adapter = new Retrofit.Builder()
.baseUrl(ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.build();
TMDBAPI apiService = adapter.create(TMDBAPI.class);
String query = movieToSearch + API_KEY;
Call<List<Movie>> call = apiService.getMovieList(query);
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Response<List<Movie>> response, Retrofit retrofit) {
List<Movie> movieList = (response.body());
}
@Override
public void onFailure(Throwable t) {
}
});
我在这里做什么?:/
我在终点添加了一个/,并将界面中的方法更改为:@GET("搜索/电影")通话<列表<电影>>getMovieList( @Query("query") String query);
i added a / to the end point, and changed the method in the interface to this:
@GET("search/movie")
Call<List<Movie>> getMovieList( @Query("query") String query);
现在的问题是,响应的body = null,在rawResponse中,它有一条消息说=响应{protocol=http/1.1, code=401, message=Unauthorized, url=https://api.themoviedb.org/3/search/movie?query=fight%26api_key%-----
the problem now is, the response has body = null, in the rawResponse, it has a message saying = Response{protocol=http/1.1, code=401, message=Unauthorized, url=https://api.themoviedb.org/3/search/movie?query=fight%26api_key%-----
我需要设置客户端吗?
推荐答案
好的,我看到你的问题了,搜索应该是这样的:
Ok, I can see your problem, the search should be do like this:
http://api.themoviedb.org/3/search/movie?api_key=###&query=iron sky
所以,问题是你是如何形成 URL 的.
So, the problem is how are you forming the URL.
这篇关于Java: Android: Retrofit - 使用 Call but, Response{code = 401,message=unauthorized}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!