我正在使用 RoboSpice Google Http Java 客户端模块
,并且我使用 JSON
数据成功执行了 POST
请求。
问题: 我现在的问题是我必须在 POST
请求中附加 byte[]
数据,但我无法找到任何数据使用 RoboSpice Google Http Java 客户端模块
执行此操作的方法。
这是我的代码 fragment :
public class SaveUserProfile_Request extends GoogleHttpClientSpiceRequest<SaveUserProfile> {
private String apiUrl;
private JSONObject mJsonObject;
private String filePath;
public SaveUserProfile_Request(JSONObject mJsonObject, String filePath) {
super(SaveUserProfile.class);
this.apiUrl = AppConstants.GLOBAL_API_BASE_ADDRESS + AppConstants.API_SAVE_PROFILE;
this.mJsonObject = mJsonObject;
this.filePath = filePath;
}
@Override
public SaveUserProfile loadDataFromNetwork() throws IOException {
Ln.d("Call web service " + apiUrl);
HttpRequest request = getHttpRequestFactory().buildPostRequest(new GenericUrl(apiUrl),
ByteArrayContent.fromString("application/json", mJsonObject.toString()));
request.setParser(new JacksonFactory().createJsonObjectParser());
return request.execute().parseAs(getResultType());
}
}
我尝试使用 Multipart HTTP Requests
但它不起作用。
最佳答案
在这里检查这个答案 https://stackoverflow.com/a/17151284/881771 您可以在调用
之前将 FileUpload 逻辑添加到您的 loadFromNetwork 方法中request.execute().parseAs(getResultType());
关于java - 如何使用带有 JSON 数据的 RoboSpice Google Http Java 客户端模块上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21523982/