Jump to content

Test connexion internet et gestion des exceptions


tammikuu

Recommended Posts

bonjour, j'ai un problème de robustesse avec mon application,

je récupère le code html d'une page à l'aide de la fonction :


public String GetHtml(String url) {
InputStream content = null;
String contentString="problème de connexion";
try {
HttpGet httpGet = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
content = response.getEntity().getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);
String line;
StringBuilder sb =  new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
contentString = sb.toString();
return contentString;
} catch (Exception e) {
return "problème de connexion";
}
}

Tout fonctionne parfaitement sauf si le telephone n'est pas connecter à internet. je croyais résoudre le problème en testant la connexion au réseau, mais si réseau il y à, internet pas forcement (exemple wifi sur sfr free wifi avant identification ou données cellulaires désactivées).

Résultat j'ai droit à un plantage en bonne et due forme.

Comment tester la connexion à internet ou tout du moins gerer l'arret de l'activité en cas de non connexion avec le catch?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...