【问题标题】:How to upload file using RoboSpice Google Http Java Client module with JSON Data如何使用带有 JSON 数据的 RoboSpice Google Http Java 客户端模块上传文件
【发布时间】:2014-02-26 17:45:16
【问题描述】:

我正在使用RoboSpice Google Http Java Client module,并且我正在使用JSON 数据成功执行POST 请求。

问题:我的问题是现在我必须在POST 请求中附加byte[] 数据,但我无法找到任何使用RoboSpice Google Http Java Client module 的方法。

这是我的代码 sn-p:

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,但它不起作用。

【问题讨论】:

    标签: java android json robospice google-http-client


    【解决方案1】:

    在此处查看此答案https://stackoverflow.com/a/17151284/881771 您可以在调用之前将 FileUpload 逻辑添加到您的 loadFromNetwork 方法中

    request.execute().parseAs(getResultType());
    

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 2021-01-06
      • 2011-10-18
      • 2012-08-01
      • 2018-08-03
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多