解决方案 »
- 写了个安卓程序,在真机上运行很好,在虚拟机上显示出错。求大神帮助
- 关于AlertDialog显示至屏幕底部的显示问题
- 求思路 - 通过notification进入某个页面后,如何返回到主界面
- 如何让几个图片Button在一行中显示,并且间距均匀
- Android视频尺寸问题
- android3.2版本开发手机应用程序
- 关于数据存储迷糊了,标题长才能吸引注意力。。。chunan帖,您可以输入49个字符
- 音乐无法重放
- android调用webservice出现java.io.IOException
- android 实现登录,一定要用到session吗?
- 安卓系统编写shell脚本,sh文件能支持mv命令吗?
- 用android单元测试即junit,测试新浪微博API问题。。
去掉参数:HttpMultipartMode.BROWSER_COMPATIBLE 试试。我以前也碰到这样的问题过。
String code = "0";
Log.i("http", actionUrl);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 1000*30);
HttpConnectionParams.setSoTimeout(httpParameters, 1000*30);
HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(actionUrl);
MultipartEntity mpEntity = new MultipartEntity();
try {
File imageFile = new File(imagepath);
if(!imageFile.exists()) {
Log.i("http", "999");
code = "999";
}
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
Log.i("Http","KEY:" + key + ",Value:" + value );
mpEntity.addPart(key, new StringBody(value));
}
FileBody file = new FileBody(imageFile);
mpEntity.addPart("name", file);
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
code = EntityUtils.toString(response.getEntity());
System.out.println("result:" + code);
Log.i("http", code);
}
} catch (Exception e) {
return code;
}
return code;
}
这个是我昨天刚写的,可用。
需要用到2个jar包,httpmime-4.0.jar,apache-mime4j-0.6.jar
还有一种是
public static String post(String actionUrl, Map<String, String> params,
Map<String, File> files) throws IOException {
StringBuilder sb2 = new StringBuilder();
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8"; URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY); StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\""
+ entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
} DataOutputStream outStream = new DataOutputStream(
conn.getOutputStream());
outStream.write(sb.toString().getBytes());
if (files != null) {
// int i = 0;
for (Map.Entry<String, File> file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
// sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND);
sb1.append("Content-Disposition: form-data; name=\"userupfile\"; filename=\""
+ file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
} is.close();
outStream.write(LINEND.getBytes());
}
} byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush(); int res = conn.getResponseCode();
InputStream in = null;
if (res == 200) {
in = conn.getInputStream();
int ch; while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
Log.i("CAMERA", sb2.toString());
} return in == null ? null : sb2.toString();
}
希望对你有用
谢谢各位的认真回复,问题已经解决,代码没问题,是调用的时候传的有一个参数是Json格式的参数,这个传的格式和服务器不符合,所以一直不成功!