0

I am a beginner in android development, looking for how to make it verifiable code for server connection, ie if the user does not have a connection, and it connects to my server, in this case an error message will show "connection error" or nothing affair.

thank you for helping me :)

      upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File f = new File(path);


            Future uploading = Ion.with(MainActivity.this)
                    .load("http://x.x.x.x/upload_file") //->how to change this code for  to verify the connection
                   .setMultipartFile("image",f)
                    //asJsonObject()
            .asString()
    .withResponse()
                    .setCallback(new FutureCallback<Response<String>>() {
                        @Override
                        public void onCompleted(Exception e, Response<String> result) {
                            try {

                                JSONObject jobj = new JSONObject(result.getResult());
                                Toast.makeText(getApplicationContext(), jobj.getString("response"), Toast.LENGTH_SHORT).show();


                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                        }
    });
        }

    });
emile01
  • 89
  • 1
  • 3
  • 15

1 Answers1

1

Based on the information you provided, I suggest you refer to this answer. It shows you how to check if the device is connected to the internet.

After checking if a connection is available or not, you can continue normally, or display an alert message (i.e. via Toast or Snackbar) to inform the user that no connection can be made.

Hope this helps.

Community
  • 1
  • 1
Mor Paz
  • 2,088
  • 2
  • 20
  • 38