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

时间:2017-07-21 15:40:44

标签: android retrofit2 android-json

我正在使用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();
        }
    });
}

我有另一个接口和改造构建器类使用OMdb API完美地工作 以下是OMDb API接口

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);
}

改装课程

 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();
            }
        });
    } 

请帮我解决这个问题!

3 个答案:

答案 0 :(得分:2)

您的API界面应如下所示:

import { StackNavigator } from 'react-navigation'
import DeckListView from '../components/DeckListView'
import NewDeckView from '../components/NewDeckView'



export const Stack = StackNavigator({

    DeckListView : {
        screen: DeckListView,
        navigationOptions: {
            headerTitle: 'Home',
},
    },


    NewDeckView: {
        screen:NewDeckView,
        navigationOptions: {
            headerTitle: 'New Deck',
},
    },




})

请注意,查询参数为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); } &amp;不是api_key

apikey方法需要声明为 @GET之外的所有内容:

BASE_URL

答案 1 :(得分:0)

你不应该用“/”开始你的网址,而是把它放在你的基础网址

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

public interface MoviesAPI {

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

以上更改为您提供了完美的网址,请尝试一下。

答案 2 :(得分:0)

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

@GET( “电影/流行”)     调用getPopular(@Query(“api_key”)String apikey);