I have two applications, one I send long. and latitude coordinates to a php file and the other application retrieves the long. and lat. coords. In order to test and see if I could get the first working I create a function I posted the latitude and long. coords two the php service and I got them back in the same Application. I placed them in a toast to see if it works. I even implemented location listener to upload the coordinates and retrieve them in the same application to test before trying to receive them in the other application. It works fine. But when I try to use the same code in the other application for receiving the coordinates, I receive blank coordinates. I debugged it and its just blank, as if when I make the call to the server from the other application it erases the current values in the php service.

Code for placing the coordinates in application one:

public void postData(String longCord, String latCord) throws JSONException{  
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(".../android/serverFile.php");
    JSONObject json = new JSONObject();

    try {
        // JSON data:
        json.put("longitude", longCord);
        json.put("latitude", latCord);
        JSONArray postjson=new JSONArray();
        postjson.put(json);

        // Post the data:
        httppost.setHeader("json",json.toString());
        httppost.getParams().setParameter("jsonpost",postjson);
        // Execute HTTP Post Request
        //System.out.print(json);
        HttpResponse response = httpclient.execute(httppost);

        // for JSON:
        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();
                }
            }
                                    JSONObject jsonObj = new JSONObject(jsonStr);
                // grabbing the menu object 
                String longitudecord = jsonObj.getString("lon");
                String latitudecord = jsonObj.getString("lat");

                Toast.makeText( getApplicationContext(),longitudecord,Toast.LENGTH_SHORT).show();
                Toast.makeText( getApplicationContext(),latitudecord,Toast.LENGTH_SHORT).show();
        }

    }catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

php file:

<?php
$json = $_SERVER['HTTP_JSON'];
$data = json_decode($json);
$lon = $data->longitude;
$lat = $data->latitude;
$variable = array( 'lon' => "$lon", 'lat' => "$lat" );
// One JSON for both variables
echo json_encode($variable);

?>

Now when I run this code on the other application...Its the same as above minus posting the coordinates...I get lon:"" and lat:"". Sort of like by making the request it has somehow erased the info that was posted by other application. Is this the case?

 public void recieveData() throws JSONException{  
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(".../android/serverFile.php");
        try {
            HttpResponse response = httpclient.execute(httppost);
            // for JSON:
            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();
                JSONObject jsonObj = new JSONObject(jsonStr);
                // grabbing the menu object 
                String longitudecord = jsonObj.getString("lon");
                String latitudecord = jsonObj.getString("lat");

                Toast.makeText( getApplicationContext(),longitudecord,Toast.LENGTH_SHORT).show();
                Toast.makeText( getApplicationContext(),jsonStr,Toast.LENGTH_SHORT).show();
            }
link|improve this question

50% accept rate
".../android/serverFile.php" does not look like a valid URL to me – lenik 7 hours ago
I put elipses on this site so that I wouldn't share where my file is hosted...The url works. Posting and getting works for the first application. Its the second where I get blanks. – inspired 7 hours ago
feedback

1 Answer

Try to access your URL in browser and make sure your PHP script does not give any errors in the following lines (since you don't supply any JSON input in the second application):

$data = json_decode($json);
$lon = $data->longitude;
$lat = $data->latitude;

Also, it would be nice to hear, how'd you save your lat/lon on the server, so they persist between the calls.

link|improve this answer
lol..I think that is the problem. There isn't an error. I thought that the JSON input I supplied in the first application would be saved in the variable $lon and $lat on the php file. How can I save the lat/lon on the server so that they can persist? do I have to use a database? – inspired 4 hours ago
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.