java.io.EOFException:改造 2.0 中第 1 行第 1 列的输入结束

java.io.EOFException: End of input at line 1 column 1 in retrofit 2.0

不知道哪里出了问题

{
  "success": "1",
  "wallpapers": [
    {
      "id": "1",
      "image": "http://cyphersol.com/apps/ringtona/uploads/gallery/1477685052.jpg"
    },
    {
      "id": "2",
      "image": "http://cyphersol.com/apps/ringtona/uploads/gallery/14776850521.jpg"
    },
    {
      "id": "3",
      "image": "http://cyphersol.com/apps/ringtona/uploads/gallery/14776850522.jpg"
    },
    {
      "id": "4",
      "image": "http://cyphersol.com/apps/ringtona/uploads/gallery/14776850523.jpg"
    },
    {
      "id": "5",
      "image": "http://cyphersol.com/apps/ringtona/uploads/gallery/14776850524.jpg"
    }
  ]
}

我用的是retrofit2.0 界面

public interface ApiInterface {

    @POST("getImages")
    Call<WallPaperResponse> getWallpapers(@Query("id") int apiKey);

}

Api 客户

public class ApiClient {


    public static final String BASE_URL = "http://cyphersol.com/apps/ringtona/webservice/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

调用 MainActivity

 ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<WallPaperResponse> call = apiService.getWallpapers(1);
        call.enqueue(new Callback<WallPaperResponse>() {
            @Override
            public void onResponse(Call<WallPaperResponse> call, Response<WallPaperResponse> response) {
                int statusCode = response.code();
                List<WallpapersItem> wallpaper = response.body().getWallpapers();

                for (int i = 0; i < wallpaper.size(); i++) {

                    Log.e(TAG, wallpaper.get(i).getImage());

                }

               // recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.list_item_movie, getApplicationContext()));
            }

            @Override
            public void onFailure(Call<WallPaperResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });

依赖关系

// retrofit, gson
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

我想这会对你有所帮助。

KingController mWebController = KingController.getInstance(this); String apiToken = "1"; mWebController.getMainCategories(apiToken);

@GET("getImages") Call getWallpaperLis(@Header("id") String api_token);

再见 拉希德·阿里

您的网络服务需要将 ID 作为 HEADER 发送 虽然您宁愿将其作为 POST 参数发送。 因此,您的网络服务没有 return 有效响应 和错误。

让我知道这是否有效。

public interface ApiInterface {

        @GET("getImages")
        Call<WallPaperResponse> getWallpapers(@Header("id") int apiKey);

    }

P.S 这个网站有可靠的改造文档 https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor