2014-03-12 73 views
1

我正在使用couchdb服務器端的原生android應用程序。 每次我試着上傳附件記錄,它投ClientProtocolExceptionAndroid,Java通過HttpClient - HttpPut

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; 

} 

回答