通过HttpClient的CouchDB的上传图片 [英] Couchdb Upload Image via Httpclient

查看:165
本文介绍了通过HttpClient的CouchDB的上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立与的CouchDB Android应用程式,我试着上传的图片与此功能的的CouchDB 文件:

I build An Android app with couchdb, i tried to uploaded image to the couchdb document with this function:

    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

And i don't know why but every time i get ClientProtocolException

httpclient.execute(httpPut).

有人知道

推荐答案

我是用今天挣扎。
研究此之后:<一href=\"http://stackoverflow.com/questions/9852809/how-to-put-image-attachment-to-couchdb-in-android\">How把图片附件在CouchDB的Andr​​oid的?

我到底是这样的:

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

并把它称为在floowing方式:

And called it in the floowing way:

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

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

This is a good reading: http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments

这篇关于通过HttpClient的CouchDB的上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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