Retrofit 2:上传图片到服务器得到奇怪的响应
Retrofit 2: upload image to server and get strange response
我使用 Retrofit 版本 2.0.1 将图像从我的 Android phone 上传到服务器,但出现了一个奇怪的错误。
这是我的 API 接口 Post 到服务器:
@POST("/vistek/myservice.php")
Call<String> send(@Part("myFile") RequestBody file);
我的 Okttp 客户端是:
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor2 = new HttpLoggingInterceptor();
interceptor2.setLevel(HttpLoggingInterceptor.Level.HEADERS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(interceptor2);
以及我将图像发送到服务器的功能:
public void sendImage(byte[] data)
{
MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
FileUploadService service = ServiceGenerator.createService(FileUploadService.class);
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JPEG, data);
Call<String> call = service.send(requestBody);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
Log.d("Upload", "success = " + response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.d("Upload","failure: " + t.getMessage());
}
});
}
调用函数后,我得到了奇怪的响应,像这样:
������JFIF����������������C������C��������"������ ��������������������
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: ������������}��!1AQa"q2����#B�� R��$3br��
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ... ��������������������������
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: ����������w��!1AQaq"2�B���� #3R� br�
当我用 Postman:
测试 api 时,我的服务器工作正常
POST /vistek/myservice.php HTTP/1.1
Host: demo.mmlab.uit.edu.vn
Cache-Control: no-cache
Postman-Token: ba965fb1-f7b9-7344-8b8a-ab46095668d1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="myFile"; filename=""
Content-Type:
----WebKitFormBoundary7MA4YWxkTrZu0gW
请帮助我了解我的代码发生了什么。
提前致谢。
这是不是奇怪的反应。它是压缩的 (gzip)。
您似乎在 header
中添加了 "accept-encoding", "gzip"
到您的请求中。在这种情况下,您有责任解压缩响应。
所以你可以做的是:
只需从您的代码中省略 accept-encoding
header。 OkHttp
会添加它自己的 accept-encoding
header,如果服务器用 gzip 响应,那么 OkHttp 会默默地为你解压缩。
你也可以这样手动解压:
StringBuilder stringBuilder = new StringBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(response.body().getBytes());
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);
String readed;
while ((readed = in.readLine()) != null) {
stringBuilder.append(readed);
}
in.close();
reader.close();
gzis.close();
String unZippedResponse = stringBuilder.toString();
我使用 Retrofit 版本 2.0.1 将图像从我的 Android phone 上传到服务器,但出现了一个奇怪的错误。 这是我的 API 接口 Post 到服务器:
@POST("/vistek/myservice.php")
Call<String> send(@Part("myFile") RequestBody file);
我的 Okttp 客户端是:
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor2 = new HttpLoggingInterceptor();
interceptor2.setLevel(HttpLoggingInterceptor.Level.HEADERS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(interceptor2);
以及我将图像发送到服务器的功能:
public void sendImage(byte[] data)
{
MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
FileUploadService service = ServiceGenerator.createService(FileUploadService.class);
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JPEG, data);
Call<String> call = service.send(requestBody);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
Log.d("Upload", "success = " + response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.d("Upload","failure: " + t.getMessage());
}
});
}
调用函数后,我得到了奇怪的响应,像这样:
������JFIF����������������C������C��������"������ ��������������������
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: ������������}��!1AQa"q2����#B�� R��$3br��
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ... ��������������������������
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: ����������w��!1AQaq"2�B���� #3R� br�
当我用 Postman:
测试 api 时,我的服务器工作正常POST /vistek/myservice.php HTTP/1.1
Host: demo.mmlab.uit.edu.vn
Cache-Control: no-cache
Postman-Token: ba965fb1-f7b9-7344-8b8a-ab46095668d1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="myFile"; filename=""
Content-Type:
----WebKitFormBoundary7MA4YWxkTrZu0gW
请帮助我了解我的代码发生了什么。 提前致谢。
这是不是奇怪的反应。它是压缩的 (gzip)。
您似乎在 header
中添加了 "accept-encoding", "gzip"
到您的请求中。在这种情况下,您有责任解压缩响应。
所以你可以做的是:
只需从您的代码中省略 accept-encoding
header。 OkHttp
会添加它自己的 accept-encoding
header,如果服务器用 gzip 响应,那么 OkHttp 会默默地为你解压缩。
你也可以这样手动解压:
StringBuilder stringBuilder = new StringBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(response.body().getBytes());
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);
String readed;
while ((readed = in.readLine()) != null) {
stringBuilder.append(readed);
}
in.close();
reader.close();
gzis.close();
String unZippedResponse = stringBuilder.toString();