我不知道问题在哪里
{
"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'
答案 0 :(得分:1)
我认为这会对你有帮助。
KingController mWebController = KingController.getInstance(this); String apiToken =“1”; mWebController.getMainCategories(apiToken);
@GET( “getImages”) 调用getWallpaperLis(@Header(“id”)String api_token);
Regargs 拉希德·阿里
答案 1 :(得分:0)
您的网络服务要求将ID作为HEADER发送 而您宁愿将其作为POST参数发送。 因此,您的Web服务未返回有效响应 和错误。
让我知道这是否有效。
public interface ApiInterface {
@GET("getImages")
Call<WallPaperResponse> getWallpapers(@Header("id") int apiKey);
}
P.S该网站有关于retorfit的可靠文档 https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor