Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.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
无法在Android中使用改型在多部分/表单数据请求中上载多个文件或文件集合_Android_File Upload_Retrofit_Multipartform Data_Retrofit2 - Fatal编程技术网

无法在Android中使用改型在多部分/表单数据请求中上载多个文件或文件集合

无法在Android中使用改型在多部分/表单数据请求中上载多个文件或文件集合,android,file-upload,retrofit,multipartform-data,retrofit2,Android,File Upload,Retrofit,Multipartform Data,Retrofit2,我正在开发一个Android应用程序。在我的应用程序中,我需要在多部分/表单数据请求中将多个或多个文件集合上载到服务器。我正在使用改装2。我可以上传单个文件到服务器。但当我上传文件列表时,服务器端的文件值总是空的 public JsonResult CreateMemePost(IEnumerable<HttpPostedFileBase> files,CreateMemePostModel model) 这是我的改装服务界面 public interface ApiService

我正在开发一个Android应用程序。在我的应用程序中,我需要在多部分/表单数据请求中将多个或多个文件集合上载到服务器。我正在使用改装2。我可以上传单个文件到服务器。但当我上传文件列表时,服务器端的文件值总是空的

public JsonResult CreateMemePost(IEnumerable<HttpPostedFileBase> files,CreateMemePostModel model)
这是我的改装服务界面

public interface ApiService {

    @Multipart
    @POST("Troll/CreateMemePost")
    Call<ResponseBody> postMeme(@Part List<MultipartBody.Part> files, @Part("AuthToken") RequestBody authToken);
}
它正在工作并已成功上载。我很确定问题出在我的改装代码上。我的代码有什么问题?如何使用Android中的改型将多部分/表单数据请求中的文件列表上载到服务器

我也尝试过这样做,只是为了上传文件

public interface ApiService {

    @FormUrlEncoded
    @POST("Troll/CreateMemePost")
    Call<ResponseBody> postMeme(@Field("files") ArrayList<RequestBody> files);//, @Part("AuthToken") RequestBody authToken);
}
公共接口服务{
@FormUrlEncoded
@帖子(“Troll/CreateMemePost”)
调用posteme(@Field(“files”)ArrayList files);/,@Part(“AuthToken”)RequestBody AuthToken);
}
服务器端的文件计数始终为0

public JsonResult CreateMemePost(IEnumerable<HttpPostedFileBase> files,CreateMemePostModel model)
然后我尝试了另一种使用MapPart的解决方案

public interface ApiService {
    @Multipart
    @POST("Troll/CreateMemePost")
    Call<ResponseBody> postMeme(@PartMap() Map<String,RequestBody> files, @Part("AuthToken") RequestBody authToken);
}
公共接口服务{
@多部分
@帖子(“Troll/CreateMemePost”)
调用posteme(@PartMap()映射文件,@Part(“AuthToken”)RequestBody AuthToken);
}
我在活动中创建了这样的地图

final ArrayList<File> photoFiles = new ArrayList<File>();
        ArrayList<MultipartBody.Part> files = new ArrayList<MultipartBody.Part>();
        for(Bitmap bitmap: previewBitmaps)
        {
            File file = null;
            try{
                String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
                file = new File(Environment.getExternalStorageDirectory(), fileName);
                if(file.exists())
                {
                    file.delete();
                }
                OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
                os.close();
                photoFiles.add(file);
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),file);
                MultipartBody.Part partFile = MultipartBody.Part.createFormData("multipart/form-data",file.getName(),requestFile);
                files.add(partFile);
            }
            catch (Exception e)
            {
                Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }

        if(files!=null && files.size()>0)
        {
            final Retrofit retrofit = app.getApiClient().getRetrofit();
            ApiService service = app.getApiClient().getApiService();

            RequestBody authToken = RequestBody.create(MediaType.parse("multipart/form-data"), app.getAuthToken());

            Call<ResponseBody> call = service.postMeme(files,authToken);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if(photoFiles!=null && photoFiles.size()>0)
                    {
                        for(File tempFile : photoFiles)
                        {
                            if(tempFile.exists())
                            {
                                tempFile.delete();
                            }
                        }
                    }
                    if(response!=null)
                    {
                        if(response.isSuccessful())
                        {
                            Toast.makeText(getBaseContext(),"File Uploaded successfully",Toast.LENGTH_SHORT).show();
                        }
                        else if(!response.isSuccessful() && response.errorBody()!=null)
                        {
                            Toast.makeText(getBaseContext(),"Server error message",Toast.LENGTH_SHORT).show();
                            /*try{

                            }
                            catch (IOException e)
                            {
                                Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                            }*/
                        }
                    }
                    else{
                        Toast.makeText(getBaseContext(),"Response is null",Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    if(photoFiles!=null && photoFiles.size()>0)
                    {
                        for(File tempFile : photoFiles)
                        {
                            if(tempFile.exists())
                            {
                                tempFile.delete();
                            }
                        }
                    }
                    Toast.makeText(getBaseContext(),"Unable to transfer data to server",Toast.LENGTH_SHORT).show();
                }
            });
        }
 HashMap<String,RequestBody> map = new HashMap<>();
 // Other steps
 MediaType MEDIA_TYPE=MediaType.parse("multipart/form-data");
 bodyFile = RequestBody.create(MEDIA_TYPE,file);
 map.put("files",bodyFile);
HashMap map=newhashmap();
//其他步骤
MediaType MEDIA_TYPE=MediaType.parse(“多部分/表单数据”);
bodyFile=RequestBody.create(媒体类型,文件);
map.put(“文件”,bodyFile);

服务器上的文件总是空的。

最后,我找到了解决方案。我使用了RequestBody和MultipartBody.Builder。我在Github上上传了解决方案

请查看此链接-