android - 如果响应在android中包含分号,则改造response.body返回截断的字符串
问题描述
我正在尝试使用改造上传图像(这是我的第一次尝试),一切正常,但我的响应正文被截断并得到 12345 而不是“12345;fileName.jpg”。
我尝试转换response.body().toString()
但仍然无法正常工作
Something.java (实现类)
标题
import retrofit2.converter.gson.GsonConverterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
执行
File file = new File(filePath);
//creating request body for file
RequestBody requestFile = RequestBody.create(MediaType.parse(fileType), file);
//The gson builder
Gson gson = new GsonBuilder()
.setLenient()
.create();
//creating retrofit object
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(this.getString(R.string.pref_upload_url))
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
//creating our api
Api api = retrofit.create(Api.class);
//creating a call and calling the upload image method
Call<String> call = api.uploadImage(requestFile);
//finally performing the call
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
String val = response.body().toString();
if (response.body() != null && response.body().length() > 0 && response.body().charAt(0) != '0') {
Toast.makeText(getApplicationContext(), "File Uploaded Successfully...", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
api.java
package com.ericsson.wfmmobileapp.android;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
public interface Api {
@Multipart
@POST("upload.php/")
Call<String> uploadImage(@Part("file\"; filename=\"myfile.jpg\" ") RequestBody file);
}
邮递员输出
解决方案
感谢@Sayad Jafari,他提供了线索。
GsonConverterFactory
是以 JSON 格式获得响应,分号(;)是行终止符号,所以我得到响应字符串直到分号,我改变了我使用的方法,ScalarsConverterFactory
而不是GsonConverterFactory
,现在它正在工作。
旧标题
import retrofit2.converter.gson.GsonConverterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
新标题
import retrofit2.converter.scalars.ScalarsConverterFactory;
旧实现
//The gson builder
Gson gson = new GsonBuilder()
.setLenient()
.create();
//creating retrofit object
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(this.getString(R.string.pref_upload_url))
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
新实施
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(this.getString(R.string.pref_upload_url))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
推荐阅读
- powerbi - 计算资历银行
- javascript - Javascript Animate 匹配 CSS 动画
- java - QUICKSORT 不给排序数组
- python-3.x - 我的 while 循环中的布尔值如何影响循环?
- c - Visual Studio 2019 在第一次遇到时通过获取功能
- javascript - 如何在反应中将文件数据转换为二进制数据?
- java - 如何从 Java 集合中获得相同的输出顺序
- c - “如果遇到NUL,fgets不会停止”是什么意思
- reactjs - 使用单个子组件反应多步表单
- azure - 遵循教程:使用 Terraform 和 HCL 创建 Azure VM 集群时,为什么会出现“未设置必填字段”?