小弟最近在搞手机端从相册选择图片然后调用httpclient 的post 上传照片到webservice(webservice是用C#写的),但是现在的问题是上传照片的时候不成功,获取了一下返回的状态是500错误,现在不知道到底是手机端出了问题还是服务器端出了问提,求高手帮解决如何在手机端用httpclient 的post上传照片到服务器。一下是手机端的上传代码(部分):
String url = "http://192.168.1.37/wjdata/api/File";//上传图片的接口
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
//添加头部验证
postRequest.addHeader("Authorization", "Basic001:dc5c7986daef50c1e02ab09b442ee34f"):
//设置兼容
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File f = new File(path);//path为手机存储图片的路径
ContentBody contentBody = new FileBody(f,"image/jpg");
//用下面的fileEntity也不行
//FileEntity fileEntity = new FileEntity(f,"binary/octet-stream");
//fileEntity.setContentType("binary/octet-stream");
entity.addPart("upfile",contentBody);
entity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);
if(response.getStatusLine().getStatusCode()==200){
log.i(TAG,"图片上传成功");
}else{
log.i(TAG,"图片上传失败");
}catch(Exception e){
e.printStackTrace();
}
求高手。。
String url = "http://192.168.1.37/wjdata/api/File";//上传图片的接口
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
//添加头部验证
postRequest.addHeader("Authorization", "Basic001:dc5c7986daef50c1e02ab09b442ee34f"):
//设置兼容
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File f = new File(path);//path为手机存储图片的路径
ContentBody contentBody = new FileBody(f,"image/jpg");
//用下面的fileEntity也不行
//FileEntity fileEntity = new FileEntity(f,"binary/octet-stream");
//fileEntity.setContentType("binary/octet-stream");
entity.addPart("upfile",contentBody);
entity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);
if(response.getStatusLine().getStatusCode()==200){
log.i(TAG,"图片上传成功");
}else{
log.i(TAG,"图片上传失败");
}catch(Exception e){
e.printStackTrace();
}
求高手。。
解决方案 »
- 捕获不到Intent.ACTION_TIME_TICK
- OutputStream close时出现java.lang.NullPointerException,在线等,发现问题在哪儿了下次多给你点分
- 怎么在android sd卡中删除一个文件
- android AlarmManager 的问题
- android状态栏中的3G、信号等图标怎么从右边弄到左边?
- EditText自动换行
- 获取手机好???急、、
- 急需一个android中下拉抽屉的例子
- textView怎么使用selector选择器更换图片
- 求救安卓面试题目
- Android 4.0 如何将Flash插件预置到系统中
- 如何在Android源码层修改焦点边框的默认显示颜色
把他们的代码要过来,没有的话要apk也行,然后自己抓包看区别在哪儿