Android display Timeout Message in Web Service when connection or read timeout while internet connected -
hi need display message "data not received" on connection or read timeout.
i implement using java.net.sockettimeoutexception can't message if there no data received while internet connected.
i through internet connected or not.
but want when internet connected.
internet connectivity test code
public static boolean isnetworkconnected(activity activity) { connectivitymanager connectivity = (connectivitymanager) activity.getsystemservice(context.connectivity_service); if (connectivity != null) { networkinfo[] info = connectivity.getallnetworkinfo(); if (info != null) (int = 0; < info.length; i++) if (info[i].getstate() == networkinfo.state.connected) { return true; } } return false; }
code
@override protected string doinbackground(string... params) { // todo auto-generated method stub string result = ""; try { url url = new url(params[0]); httpconnection = (httpurlconnection) url.openconnection(); httpconnection.setrequestproperty("accept", "application/json"); httpconnection.setreadtimeout(10000); httpconnection.setconnecttimeout(15000); httpconnection.setrequestmethod("post"); httpconnection.setdoinput(true); httpconnection.setdooutput(true); outputstream os = httpconnection.getoutputstream(); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8")); writer.write(string.valueof(values)); writer.flush(); writer.close(); os.close(); int responsecode = httpconnection.getresponsecode(); if (responsecode == httpurlconnection.http_ok) { inputstream istream = httpconnection.getinputstream(); inputstreamreader isreader = new inputstreamreader(istream); bufferedreader br = new bufferedreader(isreader); string line; while ((line = br.readline()) != null) { result += line; } } } catch (java.net.sockettimeoutexception e) { toast.maketext(context, "network error : no data received.", toast.length_short).show(); } catch (exception e) { // todo: handle exception e.printstacktrace(); log.e("error : ", e.tostring()); } return result; }
for displaying toast:
catch (java.net.sockettimeoutexception e) { runonuithread(new runnable() { @override public void run() { toast.maketext(context, "network error : no data received.", toast.length_short).show(); } }); } catch (exception e) { runonuithread(new runnable() { @override public void run() { toast.maketext(context,e.getmessage(), toast.length_short).show(); } }); }
Comments
Post a Comment