我看到了诸如如何使用 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)