android HttpURLConnection no result -
i trying webpage android app following code.
try { //processresponse(searchrequest(edittext.gettext().tostring())); stringbuilder response = new stringbuilder(); url url = new url("http://www.google.com"); log.v("gsearch", "url :" + url.tostring()); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); if (httpurlconnection.getresponsecode() == httpurlconnection.http_ok) { log.v("gsearch", "http ok"); } log.v("gsearch", "loaded"); } catch (exception e) { log.v("web content error", "e:"+e.getmessage()); }
however, got following in logcat.
v/gsearch﹕ url :http://www.google.com v/web content error﹕ e:null
i have permission in manifest.
<uses-permission android:name="android.permission.internet" />
please give me hint.
you can idea code
try { httpurlconnection urlc = (httpurlconnection) (new url( "http://www.google.com").openconnection()); urlc.setrequestproperty("user-agent", "test"); urlc.setrequestproperty("connection", "close"); urlc.setconnecttimeout(3000); urlc.setreadtimeout(4000); urlc.connect(); return (urlc.getresponsecode() == 200); } catch (ioexception e) { return (false); } } else return false; }
here complete class
public class connectiondetector { private context _context; public connectiondetector(context context) { this._context = context; } public boolean isconnectingtointernet() { if (networkconnectivity()) { try { httpurlconnection urlc = (httpurlconnection) (new url( "http://www.google.com").openconnection()); urlc.setrequestproperty("user-agent", "test"); urlc.setrequestproperty("connection", "close"); urlc.setconnecttimeout(3000); urlc.setreadtimeout(4000); urlc.connect(); // networkcode2 = urlc.getresponsecode(); return (urlc.getresponsecode() == 200); } catch (ioexception e) { return (false); } } else return false; } private boolean networkconnectivity() { connectivitymanager cm = (connectivitymanager) _context .getsystemservice(context.connectivity_service); networkinfo networkinfo = cm.getactivenetworkinfo(); if (networkinfo != null && networkinfo.isconnected()) { return true; } return false; } }
Comments
Post a Comment