小弟最近在搞手机端从相册选择图片然后调用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();
}
求高手。。
解决方案 »
- 关于findViewById返回空指针的错误
- sqlite数据库关闭问题
- android怎么样多个listview的adapter引用同一个HashMap<String, SoftReference<Drawable>>?
- 求android入门教程
- 自定义一个textview,如何在listview中作为每一项去使用?
- Android老油条请进
- 安卓APP用web service的案例多吗?
- android 4.2 插入usb 键盘显示软键盘
- 【求指教!!】Fragment ViewPager相关问题
- 麻烦有实际工作经验的Android开发者帮我回答以下几个问题
- Android 4.0 如何将Flash插件预置到系统中
- 如何在Android源码层修改焦点边框的默认显示颜色
把他们的代码要过来,没有的话要apk也行,然后自己抓包看区别在哪儿