【问题标题】:Couchdb Upload Image via HttpclientCouchdb 通过 Httpclient 上传图片
【发布时间】:2014-03-11 14:40:10
【问题描述】:

我使用couchdb 构建了一个Android 应用程序,我尝试使用此功能将图像上传到couchdb 文档:

    public JSONObject uploadPicture(PutAttachment putAttachment) {
    JSONObject obj = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPut httpPut = new HttpPut(baseUrl() + putAttachment.getDbName() + "/" + putAttachment.getDocName() + "/attachment?rev=" + putAttachment.getRev());

        ByteArrayEntity img = new ByteArrayEntity(putAttachment.getByteImg());
        httpPut.setEntity(img);

        httpPut.setHeader("Content-Length", "" + (int) img.getContentLength());
        httpPut.setHeader("Content-type", "image/png");
        httpPut.setHeader(authenticate());
        HttpResponse response;

        response = httpclient.execute(httpPut);

        HttpEntity entity = response.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            obj = new JSONObject(convertStreamToString(instream));
            instream.close();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return obj;

}

我不知道为什么,但每次我收到ClientProtocolException

之后

httpclient.execute(httpPut).

有人知道

【问题讨论】:

    标签: java android apache curl couchdb


    【解决方案1】:

    我今天为此苦苦挣扎。 研究后:How to put image attachment to CouchDB in Android?

    我最后得到了这样的东西:

    public static HttpResponse makeUpdateRequest(String uri, Bitmap bmp) {
        try {
            HttpPut httpPut = new HttpPut(uri);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 0, stream);
            ByteArrayEntity entity = new ByteArrayEntity(stream.toByteArray());
            entity.setContentType("image/png");
            entity.setChunked(true);
            httpPut.setEntity(entity);
            return new DefaultHttpClient().execute(httpPut);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    并以流畅的方式调用它:

        HttpResponse updateResponse = makeUpdateRequest(
                AppConfig.WEB_SERVER_DB_URI + uuid + 
                "/attachment?rev=" + revId, bmp);
    

    这是一个很好的阅读: http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2012-11-25
      • 2013-05-07
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多