Wednesday, 15 April 2015

json - No internet Connection "error" on Android to avoid force quit -


I am developing an application with Internet permission.

In this case when I do not have any Internet

  Private boolean is Network Connected ()  

I tried to use it Is: Connectivity Manager = CM (Connectivity Manager) getSystemService (reference CONNECTIVITY_SERVICE); NetworkInfo Ni = cm.getActiveNetworkInfo (); If (Ni == faucet) {// There are no active networks. I description is false; } And true back; } If (Network Connect () == wrong) {New AlertDialog. Builder (this) .setTitle ("Problème") .setmases ("used to connect to the Internet when connected to the Internet") .setPositiveButton ("Quitter" New DialogInterface.OnClickListener () {public void onClick (DialogInterface Dialog, int) {finish (); System.exit (0);}}) .setIcon (android.R.drawable.ic_dialog_alert) .show (); }

So there is someone who can give some better suggestions, I am using both JSN connections, for example, if the user loses the internet in the middle of downloading, then we Which type of code should be downloaded, here is an example of my JSNO class:

  class RequestTask AsyncTask and string, string, string & gt; {@Override extends protected string doInBackground (string ... uri) {HttpClient httpclient = New DefaultHttpClient (); HTTP response response; String response string = null; Try {feedback = httpclient.execute (new HTTP gate (Yuri [0])); Threaded Positions = Response .getStatusLine (); If (statusLine.getStatusCode () == HttpStatus.SC_OK) {byteoutputtreamout = new byteOnputstream (); . Response.getEntity () writeTo (outside); Reaction string = out.trusting (); Out.close (); } Else {// closes the connection. Response.getEntity () getContent () pass () .; Throw New IOException (statusLine.getReasonPhrase ()); }} Catch (Client Protocol E) {// Tudro Handle Problems ..} Catch (IOException E) {// Tudo Handle Problems ..} Return Review String; } @Override Protected Zero on PostExecute (string result) {super.onPostExecute (results); }}  

If there are any tutorials or ideas, then it will really be helpful.

You can get this method to check network availability:

  Private Boolean network status; Private Zero Check Network Available () {networkStatus = false; Try {ConnectivityManager cm = (Connectivity Manager) getSystemService (context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getNetworkInfo (0); If (netInfo! = Null & netInfo.getState () == NetworkInfo.State.CONNECTED) {networkStatus = true; } And {netInfo = cm.getNetworkInfo (1); If (netInfo! = Null & netInfo.getState () == NetworkInfo.State.CONNECTED) networkStatus = true; }} Hold (exception e) {e.printStackTrace (); NetworkStatus = false; }}  

To get information about the connectivity or availability of the network:

  if (networkStatus == true) {// if the network is available // something to do and the other {// if the network is not available // something to do}  

Finally, add these permissions to the manifest:

  & lt; Usage-permission Android: name = "android.permission.INTERNET" /> & Lt; Usage-permission Android: name = "android.permission.ACCESS_NETWORK_STATE" />  

No comments:

Post a Comment