Android: app is crashing when network connection is not working -


my android game needs information players server. simple http request, placed inside thread.

the code using below. when server , running app works fine. when server not responding (either because it's busy or down), however, app crashes.

instead of app crashing display "network busy" message user , send him main activity, not sure how it.

i tried create dialog inside catch section, thinking show if server down, it's not showing , app crashes.

any ideas how can solve this?

public void run(){               string urlstring = "http://www.mydomain.com/?param=test";          try{                                         url url = new url(urlstring);                 httpurlconnection con = (httpurlconnection) url.openconnection();                 bufferedreader reader = new bufferedreader(new inputstreamreader(con.getinputstream()));                 line = reader.readline();            }         catch(exception e){             dialog d2 = new dialog(context);             d2.setcontentview(r.layout.dialog2);             d2.show();         }  } 

update: think found issue looking @ logcat. server down bufferedreader returning empty string, , trying use down road.

so @ first, should check response. best approach

if (urlconntection.getresponsecode() == httpurlconnection.http_ok) {   // method body } else {    /// info example toast or dialog } 

you should decide use runonuithread when want update ui non-ui thead

youractivity.this.runonuithread(new runnable() {             @override             public void run() {              }         }); 

or use handler or asynctask

when decide use asynctask, can show everything

protected void onprogressupdate(string... data) {   if(data[0].equals("error")) {     toast.maketext(activity.this, "connection busy!", toast.length_short).show();  } 

also show dialog. it's you.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -