Android 使用改型下载图像文件
我在我的Android项目中使用了1.6.0改型 请求url: 我的界面是这样的:Android 使用改型下载图像文件,android,retrofit,Android,Retrofit,我在我的Android项目中使用了1.6.0改型 请求url: 我的界面是这样的: public interface ImageService { @GET("/image/thumbs/filename/{filename}") @Streaming void getThumbs( @Path("filename") String filename, Callback<Response> callback ); }
public interface ImageService {
@GET("/image/thumbs/filename/{filename}")
@Streaming
void getThumbs(
@Path("filename") String filename,
Callback<Response> callback
);
}
公共接口图像服务{
@获取(“/image/thumbs/filename/{filename}”)
@流式
虚无(
@路径(“文件名”)字符串文件名,
回调
);
}
HTTP请求成功,但出现了一些错误
D/Retrofit(27613): ---> HTTP GET https://example.com/image/thumbs/filename/sample.png
D/Retrofit(27613): ---> END HTTP (no body)
D/Retrofit(27613): <--- HTTP 200 https://example.com/image/thumbs/filename/sample.png (451ms)
D/Retrofit(27613): : HTTP/1.1 200 OK
D/Retrofit(27613): Connection: Keep-Alive
D/Retrofit(27613): Content-Disposition: inline; filename="sample.png"
D/Retrofit(27613): Content-Type: image/png; charset=binary
D/Retrofit(27613): Date: Wed, 11 Jun 2014 06:02:31 GMT
D/Retrofit(27613): Keep-Alive: timeout=5, max=100
D/Retrofit(27613): OkHttp-Received-Millis: 1402466577134
D/Retrofit(27613): OkHttp-Response-Source: NETWORK 200
D/Retrofit(27613): OkHttp-Sent-Millis: 1402466577027
D/Retrofit(27613): Server: Apache/2.2.22 (Ubuntu)
D/Retrofit(27613): Transfer-Encoding: chunked
D/Retrofit(27613): X-Powered-By: PHP/5.4.28-1+deb.sury.org~precise+1
D/Retrofit(27613): ---- ERROR https://example.com/image/thumbs/filename/sample.png
D/Retrofit(27613): java.io.UnsupportedEncodingException: binary
D/Retrofit(27613): at java.nio.charset.Charset.forNameUEE(Charset.java:322)
D/Retrofit(27613): at java.lang.String.<init>(String.java:228)
D/Retrofit(27613): at retrofit.RestAdapter.logAndReplaceResponse(RestAdapter.java:478)
D/Retrofit(27613): at retrofit.RestAdapter.access$500(RestAdapter.java:109)
D/Retrofit(27613): at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:333)
D/Retrofit(27613): at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
D/Retrofit(27613): at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
D/Retrofit(27613): at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
D/Retrofit(27613): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
D/Retrofit(27613): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
D/Retrofit(27613): at retrofit.Platform$Android$2$1.run(Platform.java:142)
D/Retrofit(27613): at java.lang.Thread.run(Thread.java:841)
D/Retrofit(27613): Caused by: java.nio.charset.UnsupportedCharsetException: binary
D/Retrofit(27613): at java.nio.charset.Charset.forName(Charset.java:309)
D/Retrofit(27613): at java.nio.charset.Charset.forNameUEE(Charset.java:320)
D/Retrofit(27613): ... 11 more
D/Retrofit(27613): ---- END ERROR
D/改装(27613):-->HTTP GEThttps://example.com/image/thumbs/filename/sample.png
D/改装(27613):-->结束HTTP(无正文)
D/改装(27613):问题在于响应上的内容类型标题包含伪字符集:
Content-Type: image/png; charset=binary
改造看到了这一点,并推断响应是可以记录的文本。您应该向服务器管理员报告问题
如果您将此问题报告给GitHub上的Reformation的问题跟踪程序,我们可能可以从该问题中恢复,而不是崩溃。另一种方法是关闭完整日志记录,这样Reformation.RestAdapter.LogandReplaceSponse将不会试图消耗响应主体当然,我们通常使用毕加索加载图像,但有时我们确实需要使用改型来加载特殊图像(如获取验证码图像),您需要为请求添加一些标头,从响应标头中获取一些值(当然,您也可以使用Picasso+OkHttp,但在一个项目中,您已经使用改型来处理大多数网络请求),所以这里介绍如何通过改造2.0.0来实现(我已经在我的项目中实现了)
关键点是您需要使用okhttp3.ResponseBody
来接收响应,否则改型将把响应数据解析为JSON,而不是二进制数据
代码:
public interface Api {
// don't need add 'Content-Type' header, it not works for Retrofit 2.0.0
// @Headers({"Content-Type: image/png"})
@GET
Call<ResponseBody> fetchCaptcha(@Url String url);
}
Call<ResponseBody> call = api.fetchCaptcha(url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
// display the image data in a ImageView or save it
Bitmap bm = BitmapFactory.decodeStream(response.body().byteStream());
ivCaptcha.setImageBitmap(bm);
} else {
// TODO
}
} else {
// TODO
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
// TODO
}
});
公共接口Api{
//不需要添加“内容类型”标题,它不适用于改装2.0.0
//@Headers({“内容类型:image/png”})
@得到
调用fetchCaptcha(@Url字符串Url);
}
Call Call=api.fetchCaptcha(url);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
if(response.body()!=null){
//在ImageView中显示或保存图像数据
位图bm=BitmapFactory.decodeStream(response.body().ByTestStream());
ivCaptcha.setImageBitmap(bm);
}否则{
//待办事项
}
}否则{
//待办事项
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
//待办事项
}
});
最近我正在玩rxjava并进行改装,这里有一个快速演示。
Talk很便宜,直接向您展示我的代码,希望对您有所帮助
public interface ImageService {
String ENDPOINT = "HTTP://REPLACE.ME";
@GET
@Streaming
Observable<Bitmap> getThumbs(@Url String filepath);
/********
* Helper class that sets up a new services
*******/
class Instance {
static ImageService instance;
public static ImageService get() {
if (instance == null)
instance = newImageService();
return instance;
}
public static ImageService newImageService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ImageService.ENDPOINT)
.addConverterFactory(BitmapConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(ImageService.class);
}
}
}
干杯,
vanvency也许可以看看square也制作了一个图像下载库。否则你应该在改型时查看TypedFile我有其他API使用改型,如果需要加载以查看mayebe,我将使用毕加索,谢谢你的建议!谢谢服务器端删除“charset=binary”,就行了!什么是BitmapFactory
?@DimitriKopriwa BitmapFactory是一些生成位图的util方法集合。
public final class BitmapConverterFactory extends Converter.Factory {
public static BitmapConverterFactory create() {
return new BitmapConverterFactory();
}
private BitmapConverterFactory() {
}
@Override
public Converter<ResponseBody, Bitmap> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
if (type == Bitmap.class) {
return new Converter<ResponseBody, Bitmap>(){
@Override
public Bitmap convert(ResponseBody value) throws IOException {
return BitmapFactory.decodeStream(value.byteStream());
}
};
} else {
return null;
}
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return null;
}
}
final RETROFIT_VERSION = '2.0.0-beta3'
compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"
compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION"