在我的应用程序中登录/注册后,我尝试调用get路由,这将获取一些数据,但如果我点击按钮我会得到一个
onResponse:Response {protocol = http / 1.1,code = 401, 消息=未经授权, URL = http://192.168.3.15/myApp/public/api/getImage}
我的电话看起来像这样:
ApiService service;
TokenManager tokenManager;
Call<ImageResponse> call;
....
tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE));
service = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManager);
....
@OnClick(R.id.testImageButton)
void getImages() {
call = service.getStreamBase64();
call.enqueue(new Callback<ImageResponse>() {
@Override
public void onResponse(Call<ImageResponse> call, Response<ImageResponse> response) {
Log.w(CLASS_LABEL, "onResponse: " + response );
}
@Override
public void onFailure(Call<ImageResponse> call, Throwable t) {
Log.w(CLASS_LABEL, "onFailure: " + t.getMessage() );
}
});
}
我的图像模型:
public class Image {
int id;
String image;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
我的图片回复:
public class ImageResponse {
List<Image> data;
public List<Image> getData() {
return data;
}
}
我确实测试了邮递员的路线,这种路线很有效。
我的api是用laravel构建的,get路由调用这个方法:
public function getImage()
{
$data = DB::table('images')->first();
return response()->json(['images' => $data ], 200, []);
}
api路线:
Route::get('image', ['as' => 'image', 'uses' => 'Api\ApiController@image']);