在android中发送包含图像和文本的多部分表单数据
我有代码在一个请求中向服务器发送文本和多个图像,我的情况是,我通过本地数据库循环获取数据和多个图像,这样我就可以使用在android中发送包含图像和文本的多部分表单数据,android,multipartform-data,multipartentity,Android,Multipartform Data,Multipartentity,我有代码在一个请求中向服务器发送文本和多个图像,我的情况是,我通过本地数据库循环获取数据和多个图像,这样我就可以使用addPart(key,textdata)用于文本数据和添加部分(键、文件名、输入流、“应用程序/八位字节流”)用于图像数据。但问题是,当我在一个请求中有多个图像时,我只能发送其中一个。这是我的全部。主要问题发生在第31行,当我有多个图像时,它只发送其中一个图像。我将感谢任何帮助。谢谢。我想问题可能在这里 reqEntity.addPart("myFile", yourImage
addPart(key,textdata)代码>用于文本数据和添加部分(键、文件名、输入流、“应用程序/八位字节流”)代码>用于图像数据。但问题是,当我在一个请求中有多个图像时,我只能发送其中一个。这是我的全部。主要问题发生在第31行,当我有多个图像时,它只发送其中一个图像。我将感谢任何帮助。谢谢。我想问题可能在这里
reqEntity.addPart("myFile", yourImage);
在这种情况下,所有图像的密钥(myFile)保持不变。因此,当您的图像不止一个时,它会继续覆盖上一个图像。所以我认为,您应该使用键附加索引(从0、1开始,等等),例如
reqEntity.addPart("myFile_"+i, yourImage);
并将图像计数与图像一起发送到服务器,这样,它将了解您实际发送的图像数量,通过在服务器端使用一个简单的for循环,他们将能够获得所有这些图像。希望这有帮助。试试这种方法
String sResponse = "";
String url = "http://www.api.in/rpcs/uploadfiles/?";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
options1.inPreferredConfig = Bitmap.Config.ARGB_8888;
byte[] data1 = null,data2= null,data3= null,data4= null,data5= null;
if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_0"))
{ up_image1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_0", "");
bitmap = BitmapFactory.decodeFile(up_image1, options1);
bitmap.compress(CompressFormat.JPEG, 100, bos1);
data1 = bos1.toByteArray();
}
if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_1"))
{ up_image2 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_1", "");
bitmap = BitmapFactory.decodeFile(up_image2, options1);
bitmap.compress(CompressFormat.JPEG, 100, bos2);
data2 = bos2.toByteArray();
}
if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_2"))
{ up_image3 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_2", "");
bitmap = BitmapFactory.decodeFile(up_image3, options1);
bitmap.compress(CompressFormat.JPEG, 100, bos3);
data3 = bos3.toByteArray();
}
if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_3"))
{ up_image4 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_3", "");
bitmap = BitmapFactory.decodeFile(up_image4, options1);
bitmap.compress(CompressFormat.JPEG, 100, bos4);
data4 = bos4.toByteArray();
}
if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_4"))
{ up_image5 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_4", "");
bitmap = BitmapFactory.decodeFile(up_image5, options1);
bitmap.compress(CompressFormat.JPEG, 100, bos5);
data5 = bos5.toByteArray();
}
entity.addPart("post_id", new StringBody(post_id));
entity.addPart("user_id", new StringBody(user_id));
entity.addPart("cat_id", new StringBody(category));
if(data1!=null){
entity.addPart("files[]", new ByteArrayBody(data1,"image/jpeg", "u1.jpg"));
}
if(data2!=null){
entity.addPart("files[]", new ByteArrayBody(data2,"image/jpeg", "u2.jpg"));
}
if(data3!=null){
entity.addPart("files[]", new ByteArrayBody(data3,"image/jpeg", "u3.jpg"));
}
if(data4!=null){
entity.addPart("files[]", new ByteArrayBody(data4,"image/jpeg", "u4.jpg"));
}
if(data5!=null){
entity.addPart("files[]", new ByteArrayBody(data5,"image/jpeg", "u5.jpg"));
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null)
{
s = s.append(sResponse);
}
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
return s.toString();
}else
{
return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
}
谢谢,我已经修改了一些代码,你的建议真的很有用!很高兴能帮助你……)