使用 Retrofit 获取 base64 图像错误 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Get base64 image using Retrofit error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
我正在尝试从经过身份验证的站点下载图像。该站点 returns base64 版本的图像。这是改造的正确方法吗?如何获取图像并设置为我的图像视图。
@GET("/img/avatars/{id}")
public void getProfilePic(@Path("id") int id,
Callback<TypedByteArray> result);
我将我的 restadapter 日志记录设置为完整,响应值如下所示
���V�3��Ωw���Tw�5�vT��>8u�`�j�S�������#���%�A���"Xw��Oq������G@]éG���f�~A#lD�)<���•
不是 base64 字符串。
我试过的
customResAdapter(ImageService.class).getProfilePic(id, new Callback<TypedByteArray>() {
@Override
public void success(TypedByteArray result, Response response) {
try {
byte[] decodedString = Base64.decode(result.getBytes(), Base64.DEFAULT);
mProfilePic.setImageBitmap(BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
}
});
我不知道以下代码是否正确,但目前我收到此错误消息
com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
at line 1 column 1 path
如果您正在使用 GsonConverter
,您将始终以 JsonParseException
结束。您需要子类化 GsonConverter 并避免 JsonParseExceptions
并继续处理 Response
本身。
验证子类 Converter
中的 Json 响应会给您提示如何处理反序列化。
换句话说,如果 json 有效,则将其通过管道传输到 GsonConverter
,否则从 Base64 解码。
public class TypedByteArrayConverter extends GsonConverter {
public TypedByteArrayConverter(Gson gson) {
super(gson);
}
public TypedByteArrayConverter(Gson gson, String charset) {
super(gson, charset);
}
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
//if you are trying to deserialize POJO from json, make a supercall, otherwise convert to TypedByteArray
if(!type.getClass().isAssignableFrom(TypedByteArray.class)) {
return super.fromBody(body, type);
}else {
try {
long length = body.length();
ByteString base64 = ByteString.read(body.in(), (int) length);
return new TypedByteArray(body.mimeType(), base64.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
ByteString
来自 Okio 包
然而,使用 Retrofit 加载图像是一个相当奇怪的想法。
我正在尝试从经过身份验证的站点下载图像。该站点 returns base64 版本的图像。这是改造的正确方法吗?如何获取图像并设置为我的图像视图。
@GET("/img/avatars/{id}")
public void getProfilePic(@Path("id") int id,
Callback<TypedByteArray> result);
我将我的 restadapter 日志记录设置为完整,响应值如下所示
���V�3��Ωw���Tw�5�vT��>8u�`�j�S�������#���%�A���"Xw��Oq������G@]éG���f�~A#lD�)<���•
不是 base64 字符串。
我试过的
customResAdapter(ImageService.class).getProfilePic(id, new Callback<TypedByteArray>() {
@Override
public void success(TypedByteArray result, Response response) {
try {
byte[] decodedString = Base64.decode(result.getBytes(), Base64.DEFAULT);
mProfilePic.setImageBitmap(BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
}
});
我不知道以下代码是否正确,但目前我收到此错误消息
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path
如果您正在使用 GsonConverter
,您将始终以 JsonParseException
结束。您需要子类化 GsonConverter 并避免 JsonParseExceptions
并继续处理 Response
本身。
验证子类 Converter
中的 Json 响应会给您提示如何处理反序列化。
换句话说,如果 json 有效,则将其通过管道传输到 GsonConverter
,否则从 Base64 解码。
public class TypedByteArrayConverter extends GsonConverter {
public TypedByteArrayConverter(Gson gson) {
super(gson);
}
public TypedByteArrayConverter(Gson gson, String charset) {
super(gson, charset);
}
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
//if you are trying to deserialize POJO from json, make a supercall, otherwise convert to TypedByteArray
if(!type.getClass().isAssignableFrom(TypedByteArray.class)) {
return super.fromBody(body, type);
}else {
try {
long length = body.length();
ByteString base64 = ByteString.read(body.in(), (int) length);
return new TypedByteArray(body.mimeType(), base64.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
ByteString
来自 Okio 包
然而,使用 Retrofit 加载图像是一个相当奇怪的想法。