[英]Couchdb Upload Image via Httpclient
I build An Android app with couchdb
, i tried to uploaded image to the couchdb
document with this function: 我使用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;
}
And i don't know why but every time i get ClientProtocolException
而且我不知道为什么,但是每次我得到ClientProtocolException
After 后
httpclient.execute(httpPut).
Someone know 有人知道
I was struggling with it today. 我今天在努力。 After studying this: How to put image attachment to CouchDB in Android? 研究了以下内容之后: 如何在Android中将图像附件添加到CouchDB?
I got something like this in the end: 我最终得到了这样的东西:
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;
}
And called it in the floowing way: 并以浮动方式调用它:
HttpResponse updateResponse = makeUpdateRequest(
AppConfig.WEB_SERVER_DB_URI + uuid +
"/attachment?rev=" + revId, bmp);
This is a good reading: http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments 这是一本好书: http : //wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.