Show message on screen if there is no internet connection [duplicate]

0

I created an android app that consumes data from a rest webservice made in Php, so far everything works perfectly. What I want now is to display a message on the screen if you do not have Internet access or inform the user to connect to the Internet if he forgets. Can anyone help me with this. Thank you in advance.)

Here's my code here.

public class MainActivity extends AppCompatActivity  {
    String myJSON;
    Handler handler;
    private Context context;
    private static final String TAG_RESULTS="resultado";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_ADD ="address";

    JSONArray peoples = null;


    ArrayList<HashMap<String, String>> personList;
    ListView list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list= (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();
        getData();


    }
    public void restartActivity(View view)
    {
        // do your work Here
        Intent intent= new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
    }


    public void getData() {

        class GetDataJSON extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                    BufferedReader inputStream = null;
                    String result = null;

                    try {

                        URL url = new URL("http://10.127.127.1/ws/aula.php");
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        StringBuilder sb = new StringBuilder();
                        inputStream = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        String line = null;
                        while ((line = inputStream.readLine()) != null)
                        {
                            sb.append(line + "\n");
                        }

                        result = sb.toString();
                    } catch (Exception e) {

                    }
                    finally {
                        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                    }
                    return result;
                }
                @Override
                protected void onPostExecute(String result){
                    myJSON=result;
                    showList();
                }
            }
            GetDataJSON g = new GetDataJSON();
            g.execute();
        }

        protected void showList(){

            try {
                JSONObject jsonObj = new JSONObject(myJSON);
                peoples = jsonObj.getJSONArray(TAG_RESULTS);
                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String address = c.getString(TAG_ADD);
                    HashMap<String,String> persons = new HashMap<String,String>();
                    persons.put(TAG_ID,id);
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_ADD,address);
                    personList.add(persons);
                }


                ListAdapter adapter = new SimpleAdapter(
                        MainActivity.this, personList, R.layout.list_item,
                        new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                        new int[]{R.id.id, R.id.name, R.id.address}
                );

                list.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
            }


        }

}
    
asked by anonymous 02.06.2017 / 21:49

1 answer

0

Place this code inside onPostExecute

ConnectivityManager cm =
                (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
        if (isConnected == true){
          Toast.makeText(getApplicationContext(), "Conectado.", Toast.LENGTH_LONG).show();                
        }else{
            Toast.makeText(getApplicationContext(), "Não foi possível conectar. Cheque a internet e tente novamente.", Toast.LENGTH_LONG).show();
        }
    
02.06.2017 / 22:08