Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:59:09+00:00 2026-06-05T03:59:09+00:00

What I am trying is I get my current location (the coordinates) and i

  • 0

What I am trying is I get my current location (the coordinates) and i want to post those coordinates on a server. I have made a server using WAMP. I have written php code and the code in java but its showing me error in the POST word. Please tell me if its correct or if i can modify it!!

PHP CODE

<?php
echo 'Hello, world!';
$json = $_GET['jsonpost'];//get the post you sent...
$data = json_decode($json); //decode the 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 INTO  `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);

?>

EDITED
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 who = 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 a new HttpClient and 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 the strings as you 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);

                // for JSON retrieval:
                if(response != null)
                { 
                InputStream is = 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();
                } finally {
                try {
                is.close();
                } catch (IOException e) {
                e.printStackTrace();
                }
                }
                String jsonStr = sb.toString(); //take the string you built place in a 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 i = 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 to 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 IS 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;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:59:12+00:00Added an answer on June 5, 2026 at 3:59 am

    The main problem I can see here is that You are talking about POST but in Your android LocationService You are creating an HttpGet object:

    HttpGet httppost = new HttpGet("http://.../serverFile.php");
    

    and then from nowhere a httpost variable is used as post:

    post.setHeader("json",json.toString());
    post.getParams().setParameter("jsonpost",postjson);
    

    In PHP I’m not sure what are You trying to achieve by this:

    $_SERVER['HTTP_JSON'];
    

    cos I’m sure there is no such index. Instead call

    $_POST['jsonpost']; // <-- that is the parameter name from Your JAVA code...
    

    or

    $_GET['jsonpost']; // <-- that is the parameter name from Your JAVA code...
    

    It almost looks like You COPY + PASTE the code from similar question here (which this one should be a duplicate of!): Sending Data From Android To Server with JSON data .

    EDIT : OK, let’s assume that Your android JAVA code is OK and You are sending data through GET. Then You will have to repair Your PHP – try this:

    <?php
    echo 'Hello, world!';
    
    $json = $_GET['jsonpost']; // get the json you sent through GET...
    $data = json_decode($json); // decode the json formatted string...
    
    print_r($data); // print out the $data variable to see whether everything is OK...
    
    $devid = $data->devid;
    $longitude = $data->longitude;
    $latitude = $data->latitude;
    
    $con = mysql_connect("","","") or die('Could not connect: ' . mysql_error());    
    mysql_select_db("a5234826_ul", $con);
    
    echo "devid" +$devid;
    echo "latitude" + $latitude;
    echo "longitude" + $longitude; 
    
    $sql = "INSERT INTO  `a5234826_ul`.`locations` (
        `devid`,
        `latitude`,
        `longitude` 
    ) VALUES (
        '$Devid',
        '$latitude',  
        '$longitude'
    );";
    if (!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error());
    }
    
    mysql_close($con);
    echo json_encode($variable);
    

    You should proceed with the print_r($data); and see whether the data arrived and whether the JSON was decoded and also to see what properties it contains.

    EDIT 2 Due to posted logcat: Did You read the error from the logcat? It is so clear!!! Programmer should of course understand what an error message is saying and should thing about what he is doing, not just copy+pasting pieces of code from somewhere without thinking of it… In Your manifest.xml You need to add a permision READ_PHONE_STATE – read about permissions here: http://developer.android.com/reference/android/Manifest.permission.html and about adding permissions to manifest.xml here: http://developer.android.com/guide/topics/security/security.html#permissions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get current location of user using CLLocationCoordinate2D. I want the
I'm trying to get the current location using the iphone sdk. Here is the
I'm trying to get my current GPS accuracy by using Location.getAccuracy() . However, this
I am trying to get the name of the city of user's current location
Hi, I am trying to get the gps location using location provider and gps
I'm trying to get my current location and show it on a map. Class
I am trying to get the location of an Executable running in memory using
I am trying to get the current traffic conditions at a particular location. The
I am trying to get the user's current location on multiple controller, they are
I am trying to get the current location. For that I implement a LocationListener

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.