相关疑难解决方法(0)

使用改造下载图像文件

我在我的Android项目上使用Retrofit 1.6.0,

请求网址:

https://example.com/image/thumbs/filename/sample.png

我的界面是这样的:

public interface ImageService {
    @GET("/image/thumbs/filename/{filename}")
    @Streaming
    void getThumbs(
        @Path("filename") String filename,
        Callback<Response> callback
    );
}
Run Code Online (Sandbox Code Playgroud)

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) …
Run Code Online (Sandbox Code Playgroud)

android retrofit

7
推荐指数
2
解决办法
2万
查看次数

如何使用 Retrofit 和 Kotlin 协程下载 PDF 文件?

我看到了诸如如何使用 Retrofit 库在 Android 中下载文件之类的主题,他们使用@Streaming和 RxJava / 回调。

我有 Kotlin、协程、改造 2.6.0 和/sf/answers/3953175411/ 中的查询:

@FormUrlEncoded
@Streaming
@POST("export-pdf/")
suspend fun exportPdf(
    @Field("token") token: String
): ExportResponse
Run Code Online (Sandbox Code Playgroud)

我有一个改造客户:

retrofit = Retrofit.Builder()
    .baseUrl(SERVER_URL)
    .client(okHttpClient)
    .build()

service = retrofit.create(Api::class.java)
Run Code Online (Sandbox Code Playgroud)

如果令牌参数正确,则查询返回 PDF 文件:

%PDF-1.4
%????
...
Run Code Online (Sandbox Code Playgroud)

如果出错,它将返回带有错误描述的 JSON:

{
    "success": 0,
    "errors": {
        "message": "..."
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,ExportResponse 是一个包含 JSON 字段的数据类,POJO。

我无法访问文件数据

Response response = restAdapter.apiRequest();

try {
    //you can now get your file in the InputStream
    InputStream …
Run Code Online (Sandbox Code Playgroud)

android kotlin retrofit2 kotlin-coroutines

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×2

kotlin ×1

kotlin-coroutines ×1

retrofit ×1

retrofit2 ×1