Android:如何在Android应用程序中使用Couchbase REST API? [英] Android:How to use Couchbase REST API in android application?

查看:156
本文介绍了Android:如何在Android应用程序中使用Couchbase REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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方法并提供您的同步网关网址和Json对象

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

这篇关于Android:如何在Android应用程序中使用Couchbase REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆