Php 安卓系统中向服务器发送多幅图像的问题
我已经上传了多个图像和一些字符串数据到php服务器。如果我发送3个图像意味着url调用3次,并且第一个图像发送一次,那么第二个图像将保存到服务器上2次,第三个图像将保存3次。如何一次发送3个图像和url调用。我已经尝试过类似的方法Php 安卓系统中向服务器发送多幅图像的问题,php,android,multipartentity,Php,Android,Multipartentity,我已经上传了多个图像和一些字符串数据到php服务器。如果我发送3个图像意味着url调用3次,并且第一个图像发送一次,那么第二个图像将保存到服务器上2次,第三个图像将保存3次。如何一次发送3个图像和url调用。我已经尝试过类似的方法 class ImageUploadTask extends AsyncTask { String sResponse = null; @Override protected void onPreExecute() { // TODO Auto-
class ImageUploadTask extends AsyncTask {
String sResponse = null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(Room_addroom1.this, "Uploading",
"Please wait...", true);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
String url ="http://airbnb.abservetech.com/demo/public/mobile/hotel/roomsadd";
int i = Integer.parseInt(params[0]);
System.out.println("i---"+i);
SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
String userID = prefs.getString("userid", null);
Bitmap bitmap = decodeFile(map.get(i));
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
entity = new MultipartEntity();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
byte[] res=bos.toByteArray();
entity.addPart("room_images", new ByteArrayBody(data,
"image/jpeg", params[1]));
entity.addPart("user_id", new StringBody("52"));
entity.addPart("room_type",new StringBody( "premium"));
entity.addPart("room_prize", new StringBody("2356"));
httpPost.setEntity(entity);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
entity.writeTo(bytes);
Log.e("mainToPost", "roommainToPost----" + entity.toString());
HttpResponse response = httpClient.execute(httpPost,
localContext);
sResponse = EntityUtils.getContentCharSet(response.getEntity());
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
return sResponse;
}
@Override
protected void onPostExecute(String sResponse) {
try {
if (dialog.isShowing())
dialog.dismiss();
if (sResponse != null) {
Toast.makeText(getApplicationContext(),
sResponse + " Photo uploaded successfully",
Toast.LENGTH_SHORT).show();
count++;
if (count < map.size()) {
new ImageUploadTask().execute(count + "", "hm" + count
+ ".jpg");
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
class ImageUploadTask扩展了AsyncTask{
字符串sResponse=null;
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
dialog=ProgressDialog.show(Room\u addroom1.this,“上传”,
“请稍等……”,对);
dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
字符串url=”http://airbnb.abservetech.com/demo/public/mobile/hotel/roomsadd";
int i=Integer.parseInt(参数[0]);
System.out.println(“i----”+i);
SharedReferences prefs=GetSharedReferences(MyPREFERENCES,MODE_PRIVATE);
字符串userID=prefs.getString(“userID”,null);
位图位图=解码文件(map.get(i));
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
实体=新的多方实体();
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
bitmap.compress(bitmap.CompressFormat.JPEG,100,bos);
字节[]数据=bos.toByteArray();
字节[]res=bos.toByteArray();
entity.addPart(“房间图像”),新的ByteArrayBody(数据,
“图像/jpeg”,参数[1]);
entity.addPart(“用户id”,新StringBody(“52”);
实体。添加部分(“房间类型”,新的StringBody(“高级”);
实体。添加部分(“room_prize”,新StringBody(“2356”);
httpPost.setEntity(实体);
ByteArrayOutputStream字节=新建ByteArrayOutputStream();
实体写入(字节);
Log.e(“mainToPost”、“roommainToPost----”+entity.toString());
HttpResponse response=httpClient.execute(httpPost,
本地上下文);
sResponse=EntityUtils.getContentCharSet(response.getEntity());
}捕获(例外e){
if(dialog.isShowing())
dialog.dismise();
Log.e(e.getClass().getName(),e.getMessage(),e);
}
返回响应;
}
@凌驾
PostExecute上受保护的void(字符串响应){
试一试{
if(dialog.isShowing())
dialog.dismise();
如果(响应!=null){
Toast.makeText(getApplicationContext(),
sResponse+“照片上传成功”,
吐司。长度(短)。show();
计数++;
如果(计数
请有人帮帮我——对不起,我在你的代码中只看到一个图像。那么你是如何尝试使用这三个图像的呢?参数[1]
中有什么?实体.addPart(“房间图像”,新的byterraybody(数据,“图像/jpeg”,参数[1])
。再添加两个:实体.addPart(“房间图像2”,新的byterraybody(数据2,“image/jpeg”,params[1])
和entity.addPart(“room_images3”,new ByteArrayBody(data3,“image/jpeg”,params[1]))
。但什么是params[1]
?Kan服务器一次处理三个图像?请有人帮我一下--对不起,我在你的代码中只看到一个图像。那么你是如何尝试使用三个图像的呢?params[1]
?entity.addPart(“房间图像”,新的ByteArrayBody(数据,“图像/jpeg”,params[1])
。再添加两个:entity.addPart(“房间图像2”)“,新的ByteArrayBody(数据2,“图像/jpeg”,参数[1]))
和实体.addPart(“房间图像3”,新的ByteArrayBody(数据3,“图像/jpeg”,参数[1]))
。但是什么是参数[1]
?服务器可以同时处理三个图像吗?