Java:Android:Retrofit 2响应代码为200,response.body为null

时间:2018-07-01 20:51:37

标签: java android callback retrofit themoviedb-api

我正在尝试构建一个Android应用,该应用使用Retrofit 2,Realm和The Movie Database API实现协作过滤算法。 当进行我的Retrofit回调时,onResponse返回成功的状态码(200),但是从日志记录中我得到我的response.body()。getResults返回null。我现在在这个泡菜中,我无法使它正常工作。我的ApiService似乎运行良好,我进行了其他改型回调,以获取导演,按标题排列的电影,按发行日期排列的电影。无论如何,这是一些代码片段可能会有所帮助。

APIService.java

package com.yannis.thesis.movierecommendationapp.api;

import com.yannis.thesis.movierecommendationapp.models.DirectorResponse;
import com.yannis.thesis.movierecommendationapp.models.GenreResponse;
import com.yannis.thesis.movierecommendationapp.models.Movie;
import com.yannis.thesis.movierecommendationapp.models.MovieResponse;
import com.yannis.thesis.movierecommendationapp.models.PrimaryMovieInfo;

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

public interface APIService {

    @POST("/list")
    Call<Movie> loadMovie();

    @GET("movie/top_rated")
    Call<MovieResponse> getTopRatedMovies(@Query("api_key") String apiKey);

    @GET("movie/popular")
    Call<MovieResponse> getPopularMovies(@Query("api_key") String apiKey);

    @GET("movie/{id}")
    Call<MovieResponse> getMovieDetails(@Path("id") int id, @Query("api_key") String apiKey);

    @GET("search/movie")
    Call<MovieResponse> getMovieByTitle(@Query("query") String title, @Query("api_key") String apiKey);
}

MovieResponse.java

package com.yannis.thesis.movierecommendationapp.models;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class MovieResponse {

    @SerializedName("page")
    private Integer page;
    @SerializedName("results")
    private List<Movie> results;
    @SerializedName("total_results")
    private Integer totalResults;
    @SerializedName("total_pages")
    private Integer totalPages;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public List<Movie> getResults() {
        return results;
    }

    public void setResults(List<Movie> results) {
        this.results = results;
    }

    public Integer getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(Integer totalResults) {
        this.totalResults = totalResults;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }
}

MovieRecommendationApp.java

package com.yannis.thesis.movierecommendationapp;

import android.app.Application;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.luseen.logger.LogType;
import com.luseen.logger.Logger;
import com.weiwangcn.betterspinner.library.BetterSpinner;
import com.yannis.thesis.movierecommendationapp.activities.BaseActivity;
import com.yannis.thesis.movierecommendationapp.activities.MainActivity;
import com.yannis.thesis.movierecommendationapp.api.APIService;
import com.yannis.thesis.movierecommendationapp.models.DirectorResponse;
import com.yannis.thesis.movierecommendationapp.models.DirectorResult;
import com.yannis.thesis.movierecommendationapp.models.Genre;
import com.yannis.thesis.movierecommendationapp.models.GenreResponse;
import com.yannis.thesis.movierecommendationapp.models.MainPagerEnum;
import com.yannis.thesis.movierecommendationapp.models.Movie;
import com.yannis.thesis.movierecommendationapp.models.MovieResponse;
import com.yannis.thesis.movierecommendationapp.models.MovieRecommendedForUser;
import com.yannis.thesis.movierecommendationapp.models.Recommendation;
import com.yannis.thesis.movierecommendationapp.models.User;
import com.yannis.thesis.movierecommendationapp.models.UserRatesMovie;
import com.yannis.thesis.movierecommendationapp.MovieRecommendationApp;
import com.yannis.thesis.movierecommendationapp.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.Sort;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MovieRecommendationApp extends Application {

    private static MovieRecommendationApp instance;
    public BaseActivity lastActivity;

    String API_BASE_URL = "http://api.themoviedb.org/3/";
    private final static String API_KEY = "******************";
    private static Retrofit retrofitinstance;

    private String loggedInUserId;
    private Realm realm;
    final Double SIMILARITY_PILLOW = 0.5;
    final Double PREDICTION_PILLOW = 3.0;

    private APIService client;
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();


    Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(
                            GsonConverterFactory.create()
                    );

    Retrofit retrofit =
            builder
                    .client(
                            httpClient.build()
                    )
                    .build();

    retrofit2.Call<MovieResponse> call;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("myrealmDB.realm")
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);

        new Logger.Builder()
                .isLoggable(BuildConfig.DEBUG)
                .logType(LogType.WARN)
                .tag("Iamerror")
                .build();

        realm = Realm.getDefaultInstance();

        MovieRecommendationAlgorithm();

    }

    public static MovieRecommendationApp getInstance() {
        return instance;
    }

    public static Retrofit getRetrofitInstance() {
        return retrofitinstance;
    }

    public static String getApiKey() {
        return API_KEY;
    }


    public void MovieRecommendationAlgorithm() {
        prediction("activeUserId","637",neighbours);

    }
public void prediction(String activeUserId, String notYetRatedMovieId,ArrayList<String> neightbours) {
        Double activeAVG = avgRating(activeUserId);
        Double A = 0.0;
        Double B = 0.0;
        for (int i = 0; i < neightbours.size(); i++) {
            avgRating(neightbours.get(i));
            A = A + similarity(activeUserId, neightbours.get(i)) * (getUser_i_MovieRating(neightbours.get(i), notYetRatedMovieId) - avgRating(neightbours.get(i)));
            B = B + similarity(activeUserId, neightbours.get(i));
        }
        final Double prediction = activeAVG + A / B;
        if (prediction < PREDICTION_PILLOW) {
            return;
        }
    int movieId = Integer.parseInt(notYetRatedMovieId);
    client = retrofit.create(APIService.class);
    call = client.getMovieDetails(movieId, MovieRecommendationApp.getApiKey());
    call.enqueue(new retrofit2.Callback<MovieResponse>() {
        @Override
        public void onResponse(retrofit2.Call<MovieResponse> call, retrofit2.Response<MovieResponse> response) {
            int statusCode = response.code();
            if (response.isSuccessful() == false) {
                Logger.w("unsuccessful w status", String.valueOf(statusCode));
            } else {
    //problem is in this spot
                Logger.w( " reponse body is " + response.body().getResults());
            }

            // Logger.e("Number of movies received: " + movies.size());
        }

        @Override
        public void onFailure(retrofit2.Call<MovieResponse> call, Throwable t) {

        }
    });
}
    }

我使用邮递员仔细检查了API调用-调用https://api.themoviedb.org/3/movie/637?api_key=*********&language=en-US 给我想要的JSON正文。 谢谢您的时间和帮助。

2 个答案:

答案 0 :(得分:0)

我不知道这是否会有所帮助,但是在这里:if (response.isSuccessful() == false) 将布尔方法isSuccessful()与false进行比较是很奇怪的,该方法已经返回了布尔值。

答案 1 :(得分:0)

在电影数据库api论坛中搜索后,我发现了这个[https://www.themoviedb.org/talk/5667650ec3a36836970002bc][1] 其中指出:“我们当前支持的唯一方法是通过查询参数。”我的问题很有可能与我使用@Path参数而不是@Query参数有关。 因此,现在百万美元的问题是如何转换

@GET("movie/{id}")
    Call<MovieResponse> getMovieDetails(@Path("id") int id, @Query("api_key") String apiKey);

使用@Query注释进行呼叫。