【问题标题】:Java: Android: Retrofit - using Call but, Response{code = 401,message=unauthorized}Java: Android: Retrofit - 使用 Call 但是,Response{code = 401,message=unauthorized}
【发布时间】:2016-01-19 19:17:49
【问题描述】:

尝试使用 Retrofit 通过电影数据库 API 访问内容,但我遇到了崩溃,没有任何抛出的异常或错误消息...我是 Retrofit 的新手,但我搜索了一些文档,这就是我有(我正在使用 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("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%-----

我必须设置客户端吗?

【问题讨论】:

  • 但是错误是什么,有些异常?请添加更多信息,您是否设置了正确的权限?互联网许可?
  • 是的,我有互联网权限
  • 好吧,我认为您需要在提出要求之前进行测试,我通常使用 Postman 来完成该任务,我明确要求然后可以进行改造。 chrome.google.com/webstore/detail/postman/…
  • 好的,很酷,我试过了,效果很好

标签: java android retrofit retrofit2


【解决方案1】:

好的,我可以看到你的问题,搜索应该是这样的:

http://api.themoviedb.org/3/search/movie?api_key=###&query=iron sky

所以,问题是你是如何形成 URL 的。

【讨论】:

  • 所以在阅读后我更改了字符串 API_KEY=----- 只是为了值,并将查询和 API_KEY 作为参数传递,并使用动态标题: Call> searchMovieList(@Query("query") 字符串查询,@Header("api_key") 字符串键);但仍然无法正常工作
  • 我使用了邮递员应用程序,它在查询之后与 apikey 一起工作,但在方法中,我应该使用查询或标题,还是只使用 1 个查询?
  • 调用> getMovieList(@Query("query") 字符串查询,@Query("api_key") String api_key);
  • 通过该更改,它不会到达 call.enqueue(...) 方法。不报错,也不例外..
  • Call> call = apiService.getMovieList(query, api_key);
【解决方案2】:

我发现我做错了什么,我提出的请求的结果,在 json 中提供了更多对象...只需创建一个具有所述字段和列表的对象。

【讨论】:

    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2021-12-14
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多