Android:Json 数据未从 TMdb API 主机检索

Android: Json data is not retrieving from TMdb API host

我正在使用 tmdb API 构建电影应用程序。我正在使用改造来获取数据。我已经验证了我的 api-key 工作正常。但是没有从 TMDb 获取数据。

这是我的API界面

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface MoviesAPI {

@GET("/movie/popular")
 Call<MovieBean> getPopular(@Query("apikey")String key);
}

改造构建器方法

public void getPopularMovies() {

    OkHttpClient client = new OkHttpClient.Builder().build();
   final String api_key = getString(R.string.tmdb_api_key);
   final String BASE_URL = "http://api.themoviedb.org/3/";

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()).build();

    MoviesAPI service = retrofit.create(MoviesAPI.class);
    Call<MovieBean> call = service.getPopular(api_key);
    call.enqueue(new Callback<MovieBean>() {
        @Override
        public void onResponse(Call<MovieBean> call, Response<MovieBean> response) {
            Log.d("movietest", "onResponse() tmdb, response_success? -" + response.isSuccessful());
        }

        @Override
        public void onFailure(Call<MovieBean> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "Couldn't fetch data!", Toast.LENGTH_LONG).show();
        }
    });
}

我有另一个界面和改造构建器 class 使用 OMdb API 工作得很好 下面是OMDbAPI界面

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface MoviesAPI2 {
   @GET("/")
   public Call<MovieBean2> getInfo(@Query("apikey")String omdbkey, 
             @Query("t")String movName,@Query("plot") String plot);
}

并改造class

 private static Retrofit obj = null;
public static final String BASE_URL = "http://www.omdbapi.com/?";

public static Retrofit getInstance(){
    //Interceptor httpLogging = new
    OkHttpClient client = new OkHttpClient.Builder().build();
    if(obj==null){
        obj = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return obj;
}

 public void search_movie() {


         final String PLOT = "full";
        Log.d("movietest", "entered input " + title);
        // I get title from searchView
        Retrofit retrofit = RetrofitBuilder.getInstance();
        MoviesAPI2 service = retrofit.create(MoviesAPI2.class);
        Call<MovieBean2> call = service.getInfo(getString(R.string.omdb_api_key), title,PLOT);
        call.enqueue(new Callback<MovieBean2>() {
            @Override
            public void onResponse(Call<MovieBean2> call, Response<MovieBean2> response) {
                Log.d("movietest", "onResponse(),success" + response.isSuccessful());
                mBean = response.body();

                tvTit.setText(mBean.getTitle());
                tvDesc.setText(mBean.getPlot());
                tvactors.setText(mBean.getActors());
                Picasso.with(getApplicationContext()).load(mBean.getPoster()).into(imagView);
                Picasso.with(getApplicationContext()).load(mBean.getPoster()).into(imgView);
                Log.d("movietest", "onResponse(), bean 2=" + mBean);
            }

            @Override
            public void onFailure(Call<MovieBean2> call, Throwable t) {

                Toast.makeText(getApplicationContext(),"Could not fetch.Please try again later!",Toast.LENGTH_LONG).show();
                //t.printStackTrace();
            }
        });
    } 

请帮我解决这个问题!

你不应该用“/”开始你的 url 而应该把它放在你的基地 url

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface MoviesAPI {

@GET("movie/popular")
 Call<MovieBean> getPopular(@Query("apikey")String key);
}

以上改动为您打造了完美的url,试试吧。

您的 API 界面应如下所示:

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface MoviesAPI {

@GET("3/movie/popular")
    Call<MovieBean> getPopular(@Query("api_key") String key);
}

请注意,查询参数是 api_key 而不是 apikey

@GET 方法需要像这样声明 除了 BASE_URL 之外的所有内容将是:

final String BASE_URL = "http://api.themoviedb.org";

我做到了,您可以在 github -- shivam141296/moviesapi 上查看我的应用程序。 base_url = "http://api.themoviedb.org/3/";

@GET("movie/popular") 调用 getPopular(@Query("api_key") String apikey);