android - GPS Coordinates not posting on web -
i want post gs coordinates android phone website. created server using wamp. did webhosting using site www.000webhost.com below php code , java code... getting location on phone coordinated not being posted on website..
php code
<?php echo 'hello, world!'; $json = $_get['jsonpost'];//get post sent... $data = json_decode($json); //decode json formatted string... print_r($data); $id = $data->id; $devid = $data->devid; $latitude = $data->latitude; $longitude = $data->longitude; $service = $data->service; $con = mysql_connect("","",""); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db("a5234826_ul", $con); $devid = $_post['devid']; $latitude = $_post['latitude']; $longitude = $_post['longitude']; echo "devid" +$devid; echo "latitude" + $latitude; echo "longitude" + $longitude; $sql = "insert `a5234826_ul`.`locations` ( `id` , `devid` , `latitude` , `longitude` , `service` ) values ( null , '$devid', '$latitude', '$longitude', '$service' )"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } mysql_close($con); echo json_encode($variable); ?>
locationservice.java
@override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } public int onstartcommand(intent intent, int flags, int startid) { powermanager pm = (powermanager)getsystemservice(context.power_service); wl = pm.newwakelock(powermanager.partial_wake_lock, "lock"); wl.acquire(); context = this; final string = intent.getstringextra("who"); final locationmanager locman = (locationmanager)getsystemservice(context.location_service); final locationlistener listener = new locationlistener(){ // start location changed public void onlocationchanged(location loc) { double latitude = loc.getlatitude(); double longitude = loc.getlongitude(); // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://.../serverfile.php"); jsonobject json = new jsonobject(); telephonymanager telephonymanager = (telephonymanager)context.getsystemservice(context.telephony_service); string devid = telephonymanager.getdeviceid(); string postdata = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\",\"devid\":\""+devid+"\"}}"; try { json.put("longitude", longitude);//place each of strings did in postdata method json.put("latitude", latitude); json.put("devid", devid); jsonarray postjson=new jsonarray(); postjson.put(json); httppost.setheader("json",json.tostring()); httppost.getparams().setparameter("jsonpost",postjson); httpresponse response = httpclient.execute(httppost); // json retrieval: if(response != null) { inputstream = response.getentity().getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } string jsonstr = sb.tostring(); //take string built place in string jsonobject rec = new jsonobject(jsonstr); string longitudecord = rec.getstring("lon"); string latitudecord = rec.getstring("lat"); // ... } }catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } if (who.equals("me")){ intent = new intent(context.getpackagename()+".locationreceived"); i.putextra("lat", string.valueof(latitude)); i.putextra("longitude", string.valueof(longitude)); i.putextra("accuracy", string.valueof(loc.getaccuracy())); context.sendbroadcast(i); notification notif = new notification(); notificationmanager nm = (notificationmanager)context.getsystemservice(context.notification_service); notif.tickertext = "location found!"; notif.icon = r.drawable.ic_launcher; notif.flags = notification.flag_auto_cancel; notif.when = system.currenttimemillis(); intent notificationintent = new intent(context, testlocatoractivity.class); notificationintent.addflags(intent.flag_activity_new_task); notificationintent.putextra("lat", string.valueof(latitude)); notificationintent.putextra("longitude", string.valueof(longitude)); notificationintent.putextra("accuracy", string.valueof(loc.getaccuracy())); pendingintent contentintent = pendingintent.getactivity(context, 0, notificationintent, 0); notif.setlatesteventinfo(context, "location found!", "click open.", contentintent); nm.notify(0, notif); } else { smsmanager smsman = smsmanager.getdefault(); smsman.sendtextmessage(who, null, "http://maps.google.com/maps?q=loc:"+latitude+","+longitude, null, null); smsman.sendtextmessage(who, null, "latitude: "+latitude+"\nlongitude: "+longitude, null, null); } locman.removeupdates(this); try { wl.release(); } catch (exception e){ e.printstacktrace(); } stopself(); } public void onproviderdisabled(string provider){ } public void onproviderenabled(string provider) { //log.i(tag, "gps on"); } public void onstatuschanged(string provider, int status, bundle extras){ switch(status) { case locationprovider.out_of_service: case locationprovider.temporarily_unavailable: case locationprovider.available: break; } } }; locman.requestlocationupdates(locationmanager.gps_provider, 0, 0, listener); locman.requestlocationupdates(locationmanager.network_provider, 0, 0, listener); return 2; } }
Comments
Post a Comment