Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用android发送多张图片_Java_Php_Android_Json_Multipartentity - Fatal编程技术网

Java 如何使用android发送多张图片

Java 如何使用android发送多张图片,java,php,android,json,multipartentity,Java,Php,Android,Json,Multipartentity,您好,当我点击“发送”按钮时,我需要发送所有选定的图片(我在表格图片[]中有所有这些图片) 在PHP中,我这样做 <input id="uploadImageAct" type="file" name="uploadImageAct[]" data-max-size="2048" accept="image/x-png, image/gif, image/jpeg" style="visibility: hidden" multiple="multiple"> 在andr

您好,当我点击“发送”按钮时,我需要发送所有选定的图片(我在表格图片[]中有所有这些图片)

在PHP中,我这样做

<input id="uploadImageAct"  type="file" name="uploadImageAct[]"  data-max-size="2048"  accept="image/x-png, image/gif, image/jpeg" style="visibility: hidden" multiple="multiple">

在android中,我只对一张图片执行此操作,但我不知道如何对多张图片执行此操作(我将所有图片放在表picture[]中)


此解决方案适用于一个文件体和我的一个图像我需要一个文件体中的多个图像

    public JSONObject post(String url, ArrayList<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for(int index=0; index < nameValuePairs.size(); index++) {
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("uploadFile")) {
                // If the key equals to "image", we use FileBody to transfer the data
                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
            } else {
                // Normal string data
             Charset chars = Charset.forName("UTF-8");
                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue(),chars));
            }
        }

        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost, localContext);


    HttpEntity httpEntity = response.getEntity();
    is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    json = sb.toString();
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
    jObj = new JSONObject(json);
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}
publicJSONObject帖子(字符串url,ArrayList nameValuePairs){
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
试一试{
MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
对于(int index=0;index
如何更改此函数以便执行此操作


谢谢:)

您可以在一个请求中上载多个文件以及Android中的其他字符串参数。 为此,您必须在项目构建路径中包含2个库,然后


或者只需访问,它可能会对您有所帮助。

是否要在一个HTTP请求中发送所有图像,或者可以在单独的请求中发送每个图像吗?@mthmulders是一个HTTP请求中的所有图像是否可以为您完成此操作?@mthmulders否这是在多个输入文件中,我需要在一个输入文件中:)此解决方案是针对一个图像的FileBody,而我需要一个FileBody中的多个图像,但含义相同,您希望在一个文件体中包含多个图像,因此您必须向服务器发送一个请求,而我有一个解决方案,在多个文件体中包含多个图像,但具有相同的一个请求。。。如果你还是选择你的,那么我建议,ArrayList。拍摄arraylist中的所有图像,并将其发送到服务器。
private void doFileUpload(){

    File file1 = new File(selectedPath1);
    File file2 = new File(selectedPath2);
    String urlString = "Your server location";
    try
    {
         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(urlString);
         FileBody bin1 = new FileBody(file1);
         FileBody bin2 = new FileBody(file2);
         MultipartEntity reqEntity = new MultipartEntity();
         reqEntity.addPart("uploadedfile1", bin1);
         reqEntity.addPart("uploadedfile2", bin2);
         reqEntity.addPart("user", new StringBody("User"));
         post.setEntity(reqEntity);
         HttpResponse response = client.execute(post);
         resEntity = response.getEntity();
         final String response_str = EntityUtils.toString(resEntity);
         if (resEntity != null) {
             Log.i("RESPONSE",response_str);
             runOnUiThread(new Runnable(){
                    public void run() {
                         try {
                            res.setTextColor(Color.GREEN);
                            res.setText("n Response from server : n " + response_str);
                            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                       }
                });
         }
    }