简体   繁体   中英

How can I get a response from the server such as “Upload Successful”

In my App I am uploading Images to my company server

The issue I am struggling with is I want the server to respond with a Upload succesful or something similar after the picture is uploaded, At the moment the upload works but I want Succesful/Error response from the server, I am using koush Ion for the upload and nothing I tried is working thus far and itried googling for something but I can;t find what I am looking for

Code for Uploading Picture

private void uploadImageToServer() {
        try {
            Bitmap bitmap=ImageLoader.init().from(selectedPhoto).requestSize(1024, 1024).getBitmap();
            String encodedImage=ImageBase64.encode(bitmap);
            Log.d(TAG, encodedImage);
            CheckBox chk=findViewById(R.id.chk1);
            if (chk.isChecked()) {
                Uri.Builder builder=new Uri.Builder();
                builder.scheme("https")
                        .authority("www.smartpractice.co.za")
                        .appendPath("files-upload-phone-app.asp")
                        .appendQueryParameter("MyForm", "Yes")
                        .appendQueryParameter("ClientID", clientId)
                        .appendQueryParameter("Username", email)
                        .appendQueryParameter("Pwd", pwd)
                        .appendQueryParameter("Category", Item)
                        .appendQueryParameter("ClientName", Item2)
                        .appendQueryParameter("NoEmail", "Yes");
                myURL=builder.build().toString();
            } else {
                Uri.Builder builder4=new Uri.Builder();
                builder4.scheme("https")
                        .authority("www.smartpractice.co.za")
                        .appendPath("files-upload-phone-app.asp")
                        .appendQueryParameter("MyForm", "Yes")
                        .appendQueryParameter("ClientID", clientId)
                        .appendQueryParameter("Username", email)
                        .appendQueryParameter("Pwd", pwd)
                        .appendQueryParameter("Category", Item)
                        .appendQueryParameter("ClientName", Item2)
                        .appendQueryParameter("NoEmail", "");

                myURL=builder4.build().toString();
            }
            final ProgressDialog pd=new ProgressDialog(SecondActivity.this);
            pd.setMessage("Uploading, Please Wait....");
            pd.show();
            File file=new File(selectedPhoto);
            Ion.with(SecondActivity.this)
                    .load(myURL)
                    .uploadProgressDialog(pd)
                    .setMultipartFile("SP-LOG", "image/*", file)


                    .asString()


                    .setCallback(new FutureCallback<String>() {
                        @Override
                        public void onCompleted(Exception e, String result) {
                            Toast.makeText(SecondActivity.this, "Upload Successful", Toast.LENGTH_LONG).show();
                            pd.cancel();


                        }
                    });


        } catch (FileNotFoundException e) {
            Toast.makeText(getApplicationContext(), "No File Found", Toast.LENGTH_LONG).show();
        }

    }

Let's assume your service returns something like this after an successful image upload by calling UploadImage API:

{ 
    "message":"Upload successful",
    "status":200
}

The above response will be sent by service side API not from android.

//Your above business logic for selecting file
Ion.with(SecondActivity.this)
                    .load(myURL)
                    .uploadProgressDialog(pd)
                    .setMultipartFile("SP-LOG", "image/*", file)
                    .asJsonObject()
                    .setCallback(new FutureCallback<JsonObject>() {
                        @Override
                        public void onCompleted(Exception e, JsonObject result) {
                            String message = result.getString("message");
                            Toast.makeText(SecondActivity.this,message, Toast.LENGTH_LONG).show();\
                            pd.cancel();


                        }
                    });

or incase API only return string like this in response "Upload successful"

then you can write like this

//Your above business logic for selecting file

Ion.with(SecondActivity.this)
                    .load(myURL)
                    .uploadProgressDialog(pd)
                    .setMultipartFile("SP-LOG", "image/*", file)
                    .asString()
                    .setCallback(new FutureCallback<String>() {
                        @Override
                        public void onCompleted(Exception e, String result) {
                            Toast.makeText(SecondActivity.this,result, Toast.LENGTH_LONG).show();\
                            pd.cancel();


                        }
                    });

I hope this helps if not please feel free to ask!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM