0

我是android和couchbase的新手。我正在开发一个android应用程序,我需要调用Couchbase REST API,但我找不到任何示例代码。所以请帮助我如何在android应用程序中调用couchbase REST API我已经提到了链接但没有得到任何解决方案

4

1 回答 1

0
public static HttpPost createPostForJSONObject(
        JsonObject params, String url) {
    HttpPost post = new HttpPost(url);
    post.setEntity(createStringEntity(params));
    return post;
}

private static HttpEntity createStringEntity(JsonObject params) {
    StringEntity se = null;
    try {
        se = new StringEntity(params.toString(), "UTF-8");
        se.setContentType("application/json; charset=UTF-8");
    } catch (UnsupportedEncodingException e) {
       // Log.e(TAG, "Failed to create StringEntity", e);
      e.printStackTrace();
    }
    return se;
}

public void postData(String url,JsonObject json) {
    HttpClient httpClient = new DefaultHttpClient();
    int statusCode =0;
          HttpPost post =  createPostForJSONObject(json, url);
          try {
            HttpResponse response = httpClient.execute(post);
            statusCode= response.getStatusLine().getStatusCode();
            if(statusCode==409){
                 //do logging n fail response
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity, "UTF-8"); // its in json
                System.out.println(responseString);
             }else if(statusCode==200){
                 // success response
             }else{
                 // do logging n fail response
             }

            //String responseString=new BasicResponseHandler().handleResponse(response);
            //System.out.println(responseString);
        }catch (IOException e) {
            e.printStackTrace();
        }

}

调用 postData 方法并提供您的同步网关 url 和 Json 对象

于 2015-12-10T14:09:03.583 回答