Android:How to use Couchbase REST API in android application?

旧城冷巷雨未停 提交于 2020-01-11 14:11:22

问题


I am new to android and couchbase.I am developing an android application where i need to call Couchbase REST API but i cant find any sample code for that.so please help me how to call couchbase REST API in android applicatin I have already referred the link but not getting any solution


回答1:


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();
        }

}

Call the postData method and provide your sync-gateway url and Json Object



来源:https://stackoverflow.com/questions/34200298/androidhow-to-use-couchbase-rest-api-in-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!