用Volley创建CouchDb文档

时间:2016-07-14 15:52:41

标签: couchdb android-volley android-library android-networking nosql

您好我尝试使用Volley在CouchDb中创建一个Document但是我遇到了POST和PUT方法的问题。

String url = "http://serverurl:port/database/";

    StringRequest request = new StringRequest(Request.Method.POST
            , url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            System.out.println(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "onErrorResponse: ", error );
                }
            })
    {
    @Override
    public Map<String,String> getHeaders()throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("Accept-Charset", AppConfig.CHARSET);
        params.put("Content-Type", "application/json");
        String encoding =  Base64.encodeToString(AppConfig.SERVER_COUCHDB_AUTHENIFICATIONDATA.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
        params.put("Authorization","Basic "+ encoding);;
        return params;
    }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            map.put("VolleyTest","Hello CouchDB");
            return map;
        }


    };

    Volley.newRequestQueue(this).add(request);

我的Logcat中出现以下错误:

07-14 15:44:06.599 2003-2064/de.game_wordit.wordit E/Volley: [361] BasicNetwork.performRequest: Unexpected response code 415 for MYURL
07-14 15:44:06.603 2003-2003/de.game_wordit.wordit E/Volley: onErrorResponse: 
                                                         com.android.volley.ServerError
                                                             at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
                                                             at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

如果我尝试PUT请求

String url = "http://serverurl:port/database/";

    StringRequest request = new StringRequest(Request.Method.PUT
            , url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            System.out.println(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "onErrorResponse: ", error );
                }
            })
    {
    @Override
    public Map<String,String> getHeaders()throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("Accept-Charset", AppConfig.CHARSET);
        params.put("Content-Type", "application/json");
        String encoding =  Base64.encodeToString(AppConfig.SERVER_COUCHDB_AUTHENIFICATIONDATA.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
        params.put("Authorization","Basic "+ encoding);;
        return params;
    }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            map.put("VolleyTest","Hello CouchDB");
            return map;
        }


    };

    Volley.newRequestQueue(this).add(request);

使用PUT请求时,这是错误:

07-14 15:47:42.221 5144-5187/de.game_wordit.wordit E/Volley: [365] BasicNetwork.performRequest: Unexpected response code 412 for MYURL`07-14 15:47:42.223 5144-5144/de.game_wordit.wordit E/Volley: onErrorResponse: 
                                                         com.android.volley.ServerError
                                                             at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
                                                             at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

我也试过`JsonObjectRequest,但它也不起作用,我得到了同样的错误。

1 个答案:

答案 0 :(得分:0)

我自己想出了什么:

String url = "http://url:port/database/docname";
    Map<String, String> map = new HashMap<>();
    map.put("a","b");
    final JSONObject json= new JSONObject(map);

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT
            , url,json, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            System.out.println(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "onErrorResponse: ", error );
                    Log.e("Volley", "onErrorResponse: "+new String(error.networkResponse.data,StandardCharsets.UTF_8) );
                }
            })
    {
    @Override
    public Map<String,String> getHeaders()throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("Accept-Charset", AppConfig.CHARSET);
        params.put("Content-Type", "application/json");
        params.put("Accept","application/json");
        String encoding =  Base64.encodeToString(AppConfig.SERVER_COUCHDB_AUTHENIFICATIONDATA.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
        params.put("Authorization","Basic "+ encoding);;
        return params;
    }

        @Override
        public byte[] getBody() {
            return json.toString().getBytes();
        }
    };
    String str = new String(request.getBody(), StandardCharsets.UTF_8);
    Log.i("iii", "onCreate:" +str);
    Volley.newRequestQueue(this).add(request);

通过这种方式,您可以创建具有固定ID的文档。如果您未在URL中键入ID,程序将尝试创建数据库。通常,如果您保留Doc ID,您应该能够创建一个具有由CouchDb创建的ID的文档。但这不适合我。 但我可以忍受这个,因为我需要自己设置我的Doc ID。