我有代码在一个请求中向服务器发送文本和多个图像,我的情况是,我遍历本地数据库以获取数据和多个图像,因此我可以使用addPart(key, textdata);
来获取文本数据和{{ 1}}用于图像数据。但问题是,当我在一个请求中有多个图像时,我只能发送其中一个图像。这是我的完整code。主要问题发生在第31行,当我有多个图像时,它只发送其中一个。我将不胜感激任何帮助。谢谢。
答案 0 :(得分:1)
我认为问题可能就在这里
reqEntity.addPart("myFile", yourImage);
在此,您的密钥(myFile)对于所有图像保持不变。因此,当您的图像不止一个时,它会一直覆盖前一个图像。所以我认为,你应该用你的密钥附加索引(从0,1开始,依此类推),例如这样的东西
reqEntity.addPart("myFile_"+i, yourImage);
并且还将image_count与图像一起发送到服务器,这样,它就会知道您实际发送了多少图像,并且通过在服务器端进行简单的for循环,他们将能够获得所有这些图像。希望这有帮助。
答案 1 :(得分:0)
试试这种方式
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\"}";
}