0

I have to check the condition when there is no response from server back end as case like when i had connected my android phone to a WiFi,and shows network active in my android phone, but the problem is when WiFi(router) is not connected to any internet service that time i need a toast.

How can i check and make a toast when i don't get any response from server back end ?

Hitesh Matnani
  • 533
  • 2
  • 8
  • 26

1 Answers1

0

Call this method to check whether connected to internet or not?

  public boolean isConnectingToInternet() {
            ConnectivityManager connectivity = (ConnectivityManager) _context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
                boolean isConnected = activeNetwork != null
                        && activeNetwork.isConnected();
                return isConnected;
            }
            return false;
        }

Null check for response:

if(response == null){ 
Toast.makeText(context, "No Response", Toast.LENGTH_SHORT).show(); 
}  else{
// parse response
}
Prince
  • 496
  • 3
  • 12
  • This thing i had done already the basic think is that some time there is internet connect to other WiFi devices but that other devise dose not have any access to internet at that time when i pares or fetch any data from internet the connectivity is true but there will be no data pares so i need a toast – Hitesh Matnani Oct 02 '14 at 11:49
  • then use null check for response. If null then show toast. – Prince Oct 02 '14 at 11:57
  • Thanks like this if(response = null ) toast ? – Hitesh Matnani Oct 02 '14 at 12:00
  • thanks this is showing a warring error i had this - @Override public void onResponse(JSONArray response) { Log.d(TAG, response.toString()); hidePDialog(); if(response == null){ Toast.makeText(getActivity(), "No Response", Toast.LENGTH_SHORT).show(); } else{} – Hitesh Matnani Oct 02 '14 at 12:36
  • Add@supperWaring 'unused' on onRespones – Hitesh Matnani Oct 02 '14 at 12:42
  • ok then just add @SuppressWarnings("unchecked") above this code. It will be better for me if you write your whole code. – Prince Oct 02 '14 at 12:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62340/discussion-between-prince-and-hitesh-matnani). – Prince Oct 02 '14 at 13:35