2013-11-22 67 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請求。

相關問題