使用凌空创建 CouchDb 文档



您好,我尝试使用 Volley 在 CouchDb 中创建文档,但我在使用 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);

我在日志猫中收到以下错误:

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,但它也不起作用,我得到同样的错误。

我自己想出了一些办法:

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,程序将尝试创建数据库。通常,如果您省略了文档ID,您应该能够使用由CouchDb创建的ID创建文档。但这对我不起作用。但我可以忍受这一点,因为我需要自己设置我的文档 ID。

相关内容

  • 没有找到相关文章

最新更新