I'm trying to get location on android device But it crashes immediately after execution -
i'm trying location on android device code follows:
private void openlocation() { lm = (locationmanager)getsystemservice(context.location_service); criteria c = new criteria(); pro = lm.getbestprovider(c,false); location l = lm.getlastknownlocation(pro); if (l != null) { toast.maketext(webactivity.this, "longitude : "+string.valueof(l.getlongitude())+", latitude : "+string.valueof(l.getlatitude()), toast.length_long).show(); } else { toast.maketext(webactivity.this,"loc object null",toast.length_long).show(); } mgac = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); }
but crashes when executed. what's wrong in code, or please give me working code give me location string , want send data url using get/post request , request should run in background.
this might u,
thread thread = new thread(new runnable() { @override public void run() { try { url url = new url(url); httpurlconnection conn = (httpurlconnection) url .openconnection(); conn.setreadtimeout(10000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); // starts query conn.connect(); inputstream stream = conn.getinputstream(); string data = convertstreamtostring(stream); // u can read data here stream.close(); } catch (exception e) { } } }); thread.start();
& convertstreamtostring is,
static string convertstreamtostring(java.io.inputstream is) { java.util.scanner s = new java.util.scanner(is).usedelimiter("\\a"); return s.hasnext() ? s.next() : ""; }
this can in sending request server in background getting location i'm not sure.
Comments
Post a Comment