Android,Java通过HttpClient - HttpPut

时间:2014-03-12 23:00:50

标签: java android node.js curl couchdb

我正在使用couchdb服务器端处理本机Android应用程序。 每当我尝试将附件上传到文档时,它都会抛出ClientProtocolException

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

}

1 个答案:

答案 0 :(得分:0)

介意发布LogCat输出?

此外,这与您的问题没有直接关系,但您可能需要牢记HttpClient isn't the best library of its kind anymore