【问题标题】:Send JSON in Post with robospice google http client使用 robospice google http 客户端在 Post 中发送 JSON
【发布时间】:2013-12-07 05:25:12
【问题描述】:

我在使用 Robospice google http java 客户端创建发布请求和发送 json 时遇到问题。我的问题是,服务器收到一个空的请求数据。 (postData 中没有)

@Override
    public AjaxResult loadDataFromNetwork() throws Exception {  

        JsonHttpContent jsonHttpContent = new JsonHttpContent(new JacksonFactory(), jsonObject);

        //ByteArrayContent.fromString("application/json", jsonObject.toString())
        HttpRequest request = getHttpRequestFactory().buildPostRequest(
                new GenericUrl(baseUrl),            
                jsonHttpContent);


        request.getHeaders().setContentType("application/json");

        request.setParser(new JacksonFactory().createJsonObjectParser());

        request.setContent(jsonHttpContent);

        HttpResponse httpResponse = request.execute();

        AjaxResult result =  httpResponse.parseAs(getResultType());

        return result;
    }

提前致谢!

【问题讨论】:

  • robospice,google http java 客户端!

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


【解决方案1】:

你可以这样做:

public class SignIn_Request extends GoogleHttpClientSpiceRequest<Login> {
    private String apiUrl;
    private JSONObject mJsonObject;

    public SignIn_Request(JSONObject mJsonObject) {
    super(Login.class);
    this.apiUrl = AppConstants.GLOBAL_API_BASE_ADDRESS + AppConstants.API_SIGN_IN;
    this.mJsonObject = mJsonObject;
    }

    @Override
    public Login 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());
    }

}

将您的 JSON 转换为字节数组并将其包含在您的发布请求中。

【讨论】:

    【解决方案2】:

    我自己一直在寻找类似的解决方案,我找到了一个很好的解释,说明 Google 希望您如何格式化内容。

    我创建了 POJO 类,只是添加了一些 getter 和 setter 并将其用于数据,它似乎对我有用。

    google-http-java-client json update existing object

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2017-07-04
      • 1970-01-01
      • 2018-06-15
      • 2020-08-16
      • 2013-10-07
      • 2016-02-22
      相关资源
      最近更新 更多