如何使用带有JSON数据的RoboSpice Google Http Java客户端模块上传文件
我正在使用如何使用带有JSON数据的RoboSpice Google Http Java客户端模块上传文件,java,android,json,robospice,google-http-client,Java,Android,Json,Robospice,Google Http Client,我正在使用RoboSpice谷歌Http Java客户端模块,并且我正在使用JSON数据成功地执行POST请求 问题:我的问题是现在我必须在POST请求中附加byte[]数据,但我找不到任何方法使用RoboSpice Google Http Java客户端模块来实现这一点 这是我的代码片段: public class SaveUserProfile_Request extends GoogleHttpClientSpiceRequest<SaveUserProfile> {
RoboSpice谷歌Http Java客户端模块
,并且我正在使用JSON
数据成功地执行POST
请求
问题:我的问题是现在我必须在POST
请求中附加byte[]
数据,但我找不到任何方法使用RoboSpice Google Http Java客户端模块来实现这一点
这是我的代码片段:
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());
}
}
公共类SaveUserProfile\u请求扩展了GoogleHttpClientSpiceRequest{
私有字符串apirl;
私有JSONObject mJsonObject;
私有字符串文件路径;
公共SaveUserProfile_请求(JSONObject mJsonObject,字符串文件路径){
super(SaveUserProfile.class);
this.apirl=AppConstants.GLOBAL\u API\u BASE\u ADDRESS+AppConstants.API\u SAVE\u PROFILE;
this.mJsonObject=mJsonObject;
this.filePath=filePath;
}
@凌驾
public SaveUserProfile loadDataFromNetwork()引发IOException{
Ln.d(“调用web服务”+apiUrl);
HttpRequest请求=getHttpRequestFactory().buildPostRequest(新的GenericUrl(APIRL),
ByteArrayContent.fromString(“application/json”,mJsonObject.toString());
setParser(新的JacksonFactory().createJsonObjectParser());
返回请求.execute().parseAs(getResultType());
}
}
我尝试使用了多部分HTTP请求
,但不起作用。在这里检查这个答案
在调用之前,可以将FileUpload逻辑添加到loadFromNetwork方法中
request.execute().parseAs(getResultType());