2013-11-22 29 views
2

我有一个与Robospice谷歌HTTP客户端发送请求和发送JSON的问题。我的问题是,服务器收到一个空的请求数据。 (postData中没有)发送JSON Post with robospice谷歌http客户端

@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; 
    } 

在此先感谢!

+0

robospice,google http客户端! –

回答

0

我一直在寻找一个类似的解决方案,我发现谷歌希望你如何格式化内容的一个体面的解释。

我做了POJO类,只是添加了一些getters和setters,并用它来处理数据,它似乎对我很有用。

google-http-java-client json update existing object

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成字节数组,它包含在POST请求。