'Getting response from server on MultipartUploadRequest android

I am facing a problem with MultipartUploadRequest from the library https://github.com/gotev/android-upload-service

My codes are working fine and uploads are being done. But I am not able to handle the response sent from the server as JSON.

Here is my code:

new MultipartUploadRequest(getApplicationContext(), uploadId, callUrl)
                        .addFileToUpload(pro_img_string, "filename") //Adding file
                        .addParameter("u",uid)
                        .setNotificationConfig(new UploadNotificationConfig())
                        .setMaxRetries(2)
                        .setUtf8Charset()
                        .setDelegate(new UploadStatusDelegate() {
                            @Override
                            public void onProgress(Context context, UploadInfo uploadInfo) {
                                progDialog.setMessage("Uploading...");
                                progDialog.setCancelable(false);
                                progDialog.show();
                            }
                            @Override
                            public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {
                                progDialog.dismiss();
                                Toasty.error(getApplicationContext(),"Error occurred while uploading",Toasty.LENGTH_LONG).show();
                            }
                            @Override
                            public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
                                progDialog.dismiss();
                                //need to handle JSON response here
                            }
                            @Override
                            public void onCancelled(Context context, UploadInfo uploadInfo) {
                                Toasty.info(getApplicationContext(),"You have cancelled the upload",Toasty.LENGTH_LONG).show();
                            }
                        })
                        .startUpload();

I am not getting any clue on how to handle this ServerResponse serverResponse
Please guide to me right direction.



Solution 1:[1]

    MultipartUploadRequest(this, serverUrl = "your_url")
        .setMethod("POST")
        .addFileToUpload(
            filePath = filePath,
            parameterName = "files[]"
        ).subscribe(this, this, object : RequestObserverDelegate {
            override fun onProgress(context: Context, uploadInfo: UploadInfo) {
                Log.e("LIFECYCLE", "Progress " + uploadInfo.progressPercent)
            }

            override fun onSuccess(
                context: Context,
                uploadInfo: UploadInfo,
                serverResponse: ServerResponse,
            ) {
                Log.e("LIFECYCLE", "Success " + uploadInfo.progressPercent)
                var response = serverResponse.bodyString
            }

            override fun onError(
                context: Context,
                uploadInfo: UploadInfo,
                exception: Throwable,
            ) {
                Log.e("LIFECYCLE", "Error " + exception.message)
            }

            override fun onCompleted(context: Context, uploadInfo: UploadInfo) {
                Log.e("LIFECYCLE", "Completed ")
            }

            override fun onCompletedWhileNotObserving() {
                Log.e("LIFECYCLE", "Completed while not observing")
            }
        })

You will get the response in onSuccess.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Verma