Android multipart image upload with HttpConnectionParams deprecated in new api

后端 未结 4 1241
难免孤独
难免孤独 2020-12-11 19:05

I am using MultiPartRequester class for multipart image upload to server, but i found that some part is deprecated in this. for example HttpConnectionParams , getConnectionM

4条回答
  •  Happy的楠姐
    2020-12-11 19:44

    IMO, you should use one of the following:

    1. OkHttp

    2. Retrofit

    3. Google's Volley

    With Volley and Retrofit, yon can read my answers at Working POST Multipart Request with Volley and without HttpEntity and Retrofit - Multipart request: Required MultipartFile parameter 'file' is not present

    Regaring OkHttp, please try My GitHub sample project with basic sample code as the following:

    // Multipart request, upload file...
            Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_launcher);
            if (drawable != null) {
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
                final byte[] bitmapdata = stream.toByteArray();
    
                OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = new MultipartBuilder()
                        .type(MultipartBuilder.FORM)
                        .addPart(
                                Headers.of("Content-Disposition", "form-data; name=\"title\""),
                                RequestBody.create(null, "Sample Text Content"))
                        .addPart(
                                Headers.of("Content-Disposition", "form-data; name=\"file\"; filename=\"ic_launcher.png\""),
                                RequestBody.create(MEDIA_TYPE_PNG, bitmapdata))
                        .build();
                final Request request = new Request.Builder()
                        .url("http://192.168.1.100:20583/fileupload")
                        .post(requestBody)
                        .build();
    
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(final Request request, final IOException e) {
                        Log.e(LOG_TAG, e.toString());
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show();
                                mTextView.setText(e.toString());
                            }
                        });
                    }
    
                    @Override
                    public void onResponse(Response response) throws IOException {
                        final String message = response.toString();
                        Log.i(LOG_TAG, message);
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
                                mTextView.setText(message);
                            }
                        });
                    }
                });
            }
    

    Hope it helps!

提交回复
热议问题