简体   繁体   English

为什么Retrofit无法解决无连接 - 检查Android上的互联网连接?

[英]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. 我有一种方法可以将点发送给另一个用户,如果发生任何错误,服务器将返回代码400和字符串,我必须向用户显示。 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. 这个东西工作我这样创建它(NetworkSDK发送一个带有params的主体,并期望收到错误状态字符串,如果它发生)所以逻辑上我在onResponse方法处理我的错误(如果Networksdk收到一个带有错误字符串的对象)和如果onFailure发生,则没有错误。 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. 如果根本没有连接,如果用户关闭该片段中的互联网,它将再次转到onFailure并显示已发送点的消息。 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. 普通的我会通过状态代码执行此操作,如果状态代码是200而不是它的okey但由于某些原因我无法通过response.code()命令获取任何状态代码。 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. 我基本上需要OnFailure方法的一些想法来检测用户是否关闭了互联网并显示正确的消息。

You can check this link where is explained in detail if internet connection status is available usgin the ConnectivityManager through the Connectivity Service. 如果Internet连接状态可用,可以通过连接服务查看ConnectivityManager可以查看此链接的详细说明。

Notice that your app will need permissions for obtaining the network status: 请注意,您的应用需要获取网络状态的权限:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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