- 稠:上傳許多圖像
private void postFile() {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(ConstValue.SEARCH_BY_PICTER);
File file = new File(currentPath);
ContentBody cbFile = new FileBody(file, "image/jpeg");
MultipartEntity mpEntity = new MultipartEntity();
try {
mpEntity.addPart("fileToUpload", cbFile);
mpEntity.addPart("sys", new StringBody("android"));
mpEntity.addPart("ver", new StringBody(ConstValue.VERSION_NAME));
mpEntity.addPart("cid", new StringBody(cid));
mpEntity.addPart("page", new StringBody("1"));
mpEntity.addPart("offset", new StringBody("80"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
httppost.setEntity(mpEntity);
// System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity resEntity = response.getEntity();
// System.out.println(response.getStatusLine());
if (resEntity != null) {
try {
final String response_str = EntityUtils.toString(resEntity);
if (!response_str.contains("@@@@")) {
ConstValue.goodByPiclist = GoodXMLUtil.parseResponse(response_str);
} else {
Toast.makeText(context, "sorry,server is busy", 2000).show();
}
// Log.d("log", response_str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resEntity != null) {
try {
resEntity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
httpclient.getConnectionManager().shutdown();
}
我用罐子httpmime-4.1.2.jar,代碼是相當不錯,在三星is9023 Android等phone.but不運行,如果我的代碼有錯誤,如果是的話,你可以給我一些建議或一些很好的其他method.very非常感謝你
編輯:對不起,我已經解決了這個問題,我的代碼是正確的,和三星是好的,我的錯誤是我把圖像尺寸無效
編輯:我遇到新問題,只在三星上傳純色成功爲純白色,純黑色。其他htc android手機任何顏色即非常well.if三星is9023不信任httpmime-4.1.2.jar非常well.because在PHP中沒有收到任何數據,如果我在三星其他圖片
「但在三星is9023沒有運行,」 - 什麼問題?你有什麼錯誤嗎? – user370305