简体   繁体   English

尝试使用Retrofit2下载照片时响应正文错误

[英]Wrong Response Body while trying to download photo using Retrofit2

I'm using Retrofit 2.0.0-beta2 and I need to download some files from my PHP server. 我正在使用Retrofit 2.0.0-beta2,我需要从PHP服务器下载一些文件。 My first approach which worked was to directly use the GET method from its relative server path and I was getting the correct bytes. 我第一个可行的方法是直接从其相对的服务器路径中使用GET方法,并且获得了正确的字节。

Now I've tried something more secure that delivers the file to me based on some checks. 现在,我尝试了一些更安全的方法,该方法可以根据一些检查将文件传送给我。 It automatically fetches the file path from the DB and checks if the user session is correct. 它会自动从数据库中获取文件路径,并检查用户会话是否正确。 This works in browser tests, both Chrome PC and Chrome from Android correctly download some photos. 此功能在浏览器测试中有效,Chrome PC和Android的Chrome均可正确下载一些照片。

I'm serving the file using the X-Sendfile header like so: 我正在使用X-Sendfile标头提供文件,如下所示:

header("X-Sendfile: $file_name");
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');

The Android-side call looks like this: Android端调用如下所示:

@Streaming
@GET("/card/download")
Call<ResponseBody> getCard(@Query("filename") String filename);

All I'm getting when opening the files is the echoed text response from server. 打开文件时,我得到的只是来自服务器的回显文本响应。 Is there any way I can receive the "correct" files? 有什么办法可以接收“正确的”文件?

Apparently there was some sort of problem installing the mod. 显然,在安装mod时出现了某种问题。

I also updated OkHttp to version 2.7.0 我也将OkHttp更新到了版本2.7.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM