Trouble Sending Multipart File with Boundary via Volley

后端 未结 1 1631
无人及你
无人及你 2020-12-03 16:01

I have a customers HTTP call working using the standard apache classes but I am trying to create a custom Volley class to handle this. Here is the code for standard call:

相关标签:
1条回答
  • 2020-12-03 16:33

    This is my working sample code (only tested with small-size files):

    public class FileUploadActivity extends Activity {
    
        private final Context mContext = this;
        HttpEntity httpEntity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_file_upload);   
    
            Drawable drawable = getResources().getDrawable(R.drawable.ic_action_home);
            if (drawable != null) {
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                final byte[] bitmapdata = stream.toByteArray();
                String url = "http://10.0.2.2/api/fileupload";
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    
                // Add binary body
                if (bitmapdata != null) {
                    ContentType contentType = ContentType.create("image/png");
                    String fileName = "ic_action_home.png";
                    builder.addBinaryBody("file", bitmapdata, contentType, fileName);
                    httpEntity = builder.build();
    
                    MyRequest myRequest = new MyRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
                        @Override
                        public void onResponse(NetworkResponse response) {
                            try {                            
                                String jsonString = new String(response.data,
                                        HttpHeaderParser.parseCharset(response.headers));
                                Toast.makeText(mContext, jsonString, Toast.LENGTH_SHORT).show();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show();                        
                        }
                    }) {
                        @Override
                        public String getBodyContentType() {
                            return httpEntity.getContentType().getValue();
                        }
    
                        @Override
                        public byte[] getBody() throws AuthFailureError {
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            try {
                                httpEntity.writeTo(bos);
                            } catch (IOException e) {
                                VolleyLog.e("IOException writing to ByteArrayOutputStream");
                            }
                            return bos.toByteArray();
                        }
                    };
    
                    MySingleton.getInstance(this).addToRequestQueue(myRequest);
                }
            }
        }
    
        ...
    }
    
    public class MyRequest extends Request<NetworkResponse>
    
    0 讨论(0)
提交回复
热议问题