请教一个关于同时上传图片和文字的例子 如题,请教一个用httpclient通过post数据发送图片和文字的例子(要同时发送)分不够可以再加 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 multipart 文件上传标准 既可以传文字 又可以传文件内容 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { EditText et = (EditText)findViewById(R.id.txtMemo); try{ //获取虚幻中的文件路径 Uri uri = data.getData(); String[] proj = { MediaStore.Images.Media.DATA }; Cursor actualimagecursor = managedQuery(uri,proj,null,null,null); int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor.getString(actual_image_column_index); //获取选中的文件 File file = new File(img_path); //从基站获取当前地理位置信息 TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); int cid = 0; int lac = 0; int mcc = 0; int mnc = 0; if(gcl != null){ cid = gcl.getCid(); lac = gcl.getLac(); mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3)); mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5)); //首先创建HttpPost对象 HttpPost post= new HttpPost("http://192.168.1.123:8011/SetTrackData.aspx?op=GetImage"); MultipartEntity entity = new MultipartEntity(); //设置POST参数 entity.addPart("CID",new StringBody(cid + "")); entity.addPart("LC",new StringBody(lac + "")); entity.addPart("MMC",new StringBody(mcc + "")); entity.addPart("MNC",new StringBody(mnc + "")); entity.addPart("IMEI",new StringBody(tm.getDeviceId())); entity.addPart("IRM",new StringBody(et.getText() + "")); entity.addPart("file",new FileBody(file)); HttpClient client = new DefaultHttpClient(); HttpResponse response=client.execute(post); } ContentResolver cr = this.getContentResolver(); Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); ImageView iv = (ImageView)findViewById(R.id.iv01); iv.setImageBitmap(bitmap); et.setText(img_path); } catch(Exception e){ et.setText(e.getMessage()); } } super.onActivityResult(requestCode, resultCode, data); }这是上传代码 private void GetImage() { HttpFileCollection upFiles = Request.Files; string backName = Request.Form["FileName"] ?? "" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"; string path = Server.MapPath(@"~\DataImages\") + backName; string result = "恢复数据库失败。"; if (upFiles.Count != 0) { // 只取第 1 个文件 HttpPostedFile hpf = upFiles[0]; if (hpf != null && hpf.ContentLength > 0) { // flash 会自动发送文件名到 Request.Form["fileName"] hpf.SaveAs(path); if (File.Exists(path)) { string sqlCmd = "INSERT INTO [ImageData]([ImagePatch],[SendImageIMEI],[SendImageLng],[SendImageLat],[ImageReMark])"; sqlCmd += "Values(@ImagePatch,@SendImageIMEI,@SendImageLng,@SendImageLat,@ImageReMark)"; CellInfo ci = new CellInfo(); GeoLocation gl = new GeoLocation(); OtherMethod om = new OtherMethod(); ci.CID = Request.Form["CID"] ?? ""; ci.LAC = Request.Form["LC"] ?? ""; ci.MCC = Request.Form["MMC"] ?? "460"; ci.MNC = Request.Form["MNC"] ?? "0"; string jsonData = gl.GenerateRequestJson(ci); string resultData = om.httpSendData("http://www.google.com/loc/json", jsonData); int s = resultData.IndexOf(':'); int e = resultData.LastIndexOf(','); LocationObject lo = gl.GetObjData(resultData.Substring(s + 1, e - s)); if (!string.IsNullOrEmpty(lo.longitude)) { string[,] parms = { {"ImagePatch",path}, {"SendImageIMEI",Request.Form["IMEI"] ?? ""}, {"SendImageLng",lo.longitude}, {"SendImageLat",lo.latitude}, {"ImageReMark",Server.HtmlDecode(Request.Form["IRM"] ?? "")} }; DBOperation dbo = new DBOperation(); dbo.RunSql(sqlCmd, parms); } } } } }这是接收端代码,就是死活能接收参数不能接收文件 在BaseExpandableListAdapter下如何动态修改组名 求助 GridView 中显示图片与标题 真心求助,想了半天也想不出解决的办法 java的FileWriter问题 android中如何区分UI主线程和子线程 ContentProvider中的“完全限定的授权名”和“基础授权名称”是什么意思? 如何实现QQ登陆界面样式,请教。 android如何实现应用内付费? (新手)这个错误是什么意思? Android碎片中的多按钮的监听问题 打算开始Android开发,正在下载API,有个API版本的疑问 初学android的问题
if (resultCode == RESULT_OK) {
EditText et = (EditText)findViewById(R.id.txtMemo);
try{
//获取虚幻中的文件路径
Uri uri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
//获取选中的文件
File file = new File(img_path); //从基站获取当前地理位置信息
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
int cid = 0;
int lac = 0;
int mcc = 0;
int mnc = 0;
if(gcl != null){
cid = gcl.getCid();
lac = gcl.getLac();
mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));
mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));
//首先创建HttpPost对象
HttpPost post= new HttpPost("http://192.168.1.123:8011/SetTrackData.aspx?op=GetImage");
MultipartEntity entity = new MultipartEntity();
//设置POST参数
entity.addPart("CID",new StringBody(cid + ""));
entity.addPart("LC",new StringBody(lac + ""));
entity.addPart("MMC",new StringBody(mcc + ""));
entity.addPart("MNC",new StringBody(mnc + ""));
entity.addPart("IMEI",new StringBody(tm.getDeviceId()));
entity.addPart("IRM",new StringBody(et.getText() + ""));
entity.addPart("file",new FileBody(file));
HttpClient client = new DefaultHttpClient();
HttpResponse response=client.execute(post);
}
ContentResolver cr = this.getContentResolver();
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView iv = (ImageView)findViewById(R.id.iv01);
iv.setImageBitmap(bitmap);
et.setText(img_path);
}
catch(Exception e){
et.setText(e.getMessage());
}
}
super.onActivityResult(requestCode, resultCode, data);
}
这是上传代码 private void GetImage()
{
HttpFileCollection upFiles = Request.Files;
string backName = Request.Form["FileName"] ?? "" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
string path = Server.MapPath(@"~\DataImages\") + backName;
string result = "恢复数据库失败。"; if (upFiles.Count != 0)
{
// 只取第 1 个文件
HttpPostedFile hpf = upFiles[0];
if (hpf != null && hpf.ContentLength > 0)
{
// flash 会自动发送文件名到 Request.Form["fileName"]
hpf.SaveAs(path); if (File.Exists(path))
{
string sqlCmd = "INSERT INTO [ImageData]([ImagePatch],[SendImageIMEI],[SendImageLng],[SendImageLat],[ImageReMark])";
sqlCmd += "Values(@ImagePatch,@SendImageIMEI,@SendImageLng,@SendImageLat,@ImageReMark)"; CellInfo ci = new CellInfo();
GeoLocation gl = new GeoLocation();
OtherMethod om = new OtherMethod(); ci.CID = Request.Form["CID"] ?? "";
ci.LAC = Request.Form["LC"] ?? "";
ci.MCC = Request.Form["MMC"] ?? "460";
ci.MNC = Request.Form["MNC"] ?? "0"; string jsonData = gl.GenerateRequestJson(ci);
string resultData = om.httpSendData("http://www.google.com/loc/json", jsonData);
int s = resultData.IndexOf(':');
int e = resultData.LastIndexOf(',');
LocationObject lo = gl.GetObjData(resultData.Substring(s + 1, e - s)); if (!string.IsNullOrEmpty(lo.longitude))
{
string[,] parms = {
{"ImagePatch",path},
{"SendImageIMEI",Request.Form["IMEI"] ?? ""},
{"SendImageLng",lo.longitude},
{"SendImageLat",lo.latitude},
{"ImageReMark",Server.HtmlDecode(Request.Form["IRM"] ?? "")}
};
DBOperation dbo = new DBOperation();
dbo.RunSql(sqlCmd, parms);
}
}
}
}
}
这是接收端代码,就是死活能接收参数不能接收文件