简体   繁体   中英

Why Retrofit can't resolve No connection - Check internet connection on Android?

Hello guys I have one specific problem. I'm using retrofit library for all of my network calls. And I have one method to send points to another user and if any error occurs, the server will return me code 400 and string alongside it that I will have to show to user. This thing is working I created it like this ( NetworkSDK is sending a body with params and expect to recieve an error status string if it occurs) so logically I am processing my errors in onResponse method (if Networksdk recieve an object with error string) and if onFailure occurs there is no error. But now I have a bigger problem. If there is no connection at all, if user turns off internet in that fragment it will again go to onFailure and show message that points are sent. Ordinary I would do that via status code if status code is 200 than it's okey but for some reasons I can't get any status code via response.code() command. Here is the code :

  @Override
    public void onClick(View v) {
        try {
            final String cardNo;
            String amountstring = bodovi.getText().toString();
            final Integer amount = Integer.valueOf(amountstring);
            cardNo = brojkartice.getText().toString();
            Log.d("Broj bodova:", amount.toString());
            Log.d("Broj kartice:", cardNo);
            final SendPoints Posiljka = new SendPoints();
            Posiljka.setAmount(amount);
            Posiljka.setCardNumber(cardNo);
                NetworkSDK.getInstance().SendPoints(Posiljka, new Callback<SendPointsResponse>() {
                    @Override
                    public void onResponse(Call<SendPointsResponse> call, Response<SendPointsResponse> response) {

                        Log.d("Response code:", "" + response.code());
                        Log.d("Kartica", Posiljka.getCardNumber());
                        Log.d("Broj bodova", Posiljka.getAmount().toString());


                        try {
                            SendPointsResponse error = (new Gson()).fromJson(response.errorBody().string(), SendPointsResponse.class);

                            if (error.getCode().equals("-1"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodoava. Neispravni ulazni podaci.", Toast.LENGTH_SHORT).show();
                            if (error.getCode().equals("-2"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodova. Korisnički podaci nisu popunjeni.", Toast.LENGTH_SHORT).show();
                            if (error.getCode().equals("-3"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodova. Korisnik ima rezervisanu nagradu.", Toast.LENGTH_SHORT).show();
                            if (error.getCode().equals("-4"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodova. U toku jednog mjeseca bodove je moguće poslati maksimalno dva puta.", Toast.LENGTH_SHORT).show();
                            if (error.getCode().equals("-5"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodova. Bodove nije moguće poslati samom sebi.", Toast.LENGTH_SHORT).show();
                            if (error.getCode().equals("-6"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodova. Korisnik ne može primiti bodove.", Toast.LENGTH_SHORT).show();
                            if (error.getCode().equals("-100"))
                                Toast.makeText(getActivity(), "Neuspješno slanje bodova. Greška nepoznata.", Toast.LENGTH_SHORT).show();


                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onFailure(Call<SendPointsResponse> call, Throwable t) {

                        Toast.makeText(getActivity(), "Uspješno slanje bodova.", Toast.LENGTH_SHORT).show();
                    }
                });



        }catch (Exception e) {
            Toast.makeText(getActivity(), "Podaci nisu ispravni, provjerite podatke !", Toast.LENGTH_SHORT).show();

        }   }

});


}

I basicly need some idea in OnFailure method to detect if user is turned off internet and to display proper message.

You can check this link where is explained in detail if internet connection status is available usgin the ConnectivityManager through the Connectivity Service.

Notice that your app will need permissions for obtaining the network status:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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