gpt4 book ai didi

android - 改造 2 : upload image to server and get strange response

转载 作者:行者123 更新时间:2023-11-29 01:21:29 26 4
gpt4 key购买 nike

我使用 Retrofit 版本 2.0.1 将图像从我的 Android 手机上传到服务器,但出现了一个奇怪的错误。这是我发布到服务器的 API 接口(interface):

@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

请帮助我了解我的代码发生了什么。提前致谢。

最佳答案

这不是不是奇怪的 react 。它是压缩的 (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();

关于android - 改造 2 : upload image to server and get strange response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com