【发布时间】:2014-05-28 11:01:39
【问题描述】:
我正在尝试使用此代码在服务器上上传图像和文本,但使用此代码时出现一些错误“content-length header already present”,或者如果我没有设置内容长度,则返回“需要 411 长度"
请给我一些解决方案
这是我的代码
String url = webserviceURLs.createQuestion+"?token"+OkayUserProfileProps.tocken;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type", "multipart/form-data");
httppost.addHeader("Accept", "application/json");
byte[] abc = convertImageToByte(Uri.parse(imageName.get(0)));
InputStream in = new ByteArrayInputStream(abc);
ContentBody mimePart = new InputStreamBody(in, "file1");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("userquestion[question_id]", new StringBody("2"));
reqEntity.addPart("userquestion[looks][0][photo]", mimePart);
httppost.setEntity(reqEntity);
int a=abc.length;
httppost.setHeader(HTTP.CONTENT_LEN,""+abc.length);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String retSrc = EntityUtils.toString(resEntity);
System.out.println("Responce "+retSrc);
}
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Exception", e.getMessage() + "");
return null;
}
【问题讨论】:
-
请给我一些解决方案
标签: android web-services http-post