编译

时间:2016-04-19 03:34:38

标签: android performance retrofit retrofit2

我使用以下代码来调用API。我添加了无法使用的自定义密钥:

 String API_BASE_URL="http://api.themoviedb.org/3/movie/popular?  api_key=adfasdfasd/";

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MoviesApi service = retrofit.create(MoviesApi.class);
    Call<List<Pictures>> Pics = service.listPictures();

我在gradle中添加了以下依赖项以进行改造:

   compile 'com.squareup.retrofit2:retrofit:2.0.1'
   compile 'com.squareup.retrofit2:converter-gson:2.0.2'

这是我的界面

   public interface MoviesApi {
     @GET("results")
     Call<List<Pictures>> listPictures();
   }

我得到了:

  

引起:java.lang.IllegalArgumentException:baseUrl必须以/:http://api.themoviedb.org/3/movie/popular?api_key=adfasdfasd/结尾处于retrofit2.Retrofit $ Builder.baseUrl(Retrofit.java:496)                                                                                   at retrofit2.Retrofit $ Builder.baseUrl(Retrofit.java:439)                                                                                   在com.example.varunbehl.mymovies.MainActivity.onCreate(MainActivity.java:25)                                                                                   在android.app.Activity.performCreate(Activity.java:6251)                                                                                   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)                                                                                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)                                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)                                                                                   在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1363)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5459)                                                                                   at java.lang.reflect.Method.invoke(Native Method)

我在BASE URL的末尾添加了/。任何解决方案?

5 个答案:

答案 0 :(得分:1)

问题在于传递网址的方法:

您应该传递以下网址:

String API_BASE_URL =&#34; http://api.themoviedb.org/3/;

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
MoviesApi service = retrofit.create(MoviesApi.class);
Call<List<Pictures>> Pics = service.listPictures();

你的界面就像这样:

     public interface MoviesApi {
     @GET("/movie/popular?{api_key}/results")
     Response getMovieList(@Path("api_key") String api_key, Callback<Response> callback);

希望它会对你有所帮助。

答案 1 :(得分:0)

首先,我会仔细检查API文档,并确保您的查询格式正确。

http://docs.themoviedb.apiary.io/#reference/discover/discovermovie/get

他们有如下所示的热门电影:

http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key= 您的API密钥就在这里

我会重新开始使用基本网址:

.baseurl(http://api.themoviedb.org/3/discover/movie

然后附加您的查询参数。

答案 2 :(得分:0)

 String API_BASE_URL="http://api.themoviedb.org/";

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
MoviesApi service = retrofit.create(MoviesApi.class);
Call<List<Pictures>> Pics = service.listPictures();

public interface MoviesApi {
 @GET("/3/movie/popular")
 Call<List<Pictures>> listPictures(@Query("api_key")String api_key);
}

答案 3 :(得分:0)

我认为问题是您的基本网址中包含查询参数。因此,当您尝试按照设置的方式使用改造时,您定义的路线将是这样的

http://api.themoviedb.org/3/movie/popular?api_key=adfasdfasd/results

这不是您网址的有效结构,因为查询参数必须是其最后一部分。

最好使用最高级别的URL而不使用任何实际路由作为您的BASE_URL,所以 为了使这项工作,您可以尝试这样的事情:

String API_BASE_URL="http://api.themoviedb.org/3/";
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
MoviesApi service = retrofit.create(MoviesApi.class);
Call<List<Pictures>> Pics = service.listPictures();

然后您的界面将如下所示:

public interface MoviesApi {
   @GET("movie/popular/results")
   Call<List<Pictures>> listPictures(@Query("api_key") String api_key);

}

您可能还想验证您的路线确实是正确的。如果您的接口中的路由定义不正确,您将无法从API获得正确的响应。

答案 4 :(得分:0)

就我而言,基本网址必须是“https://restcountries.eu/rest/v1/name/england”。它在域之后不能有任何段和参数。

然后你可以定义端点:

public interface MoviesApi {
   @GET("/3/movie/popular?api_key=adfasdfasd/")
   Call<List<Pictures>> listPictures(@Query("api_key") String api_key);