• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

using HttpConnection class to "post" data to website

Hi ,
I am trying to send gps coordinates using the HttpConnection class in a java ME application.
the java code is below.
The application is installed on an nokia s60 device and gives the user 2 options  with the coordinates..
1/send to  a predefined mobile phone...this bit works fine
2/ send to a url, http://blossompark.net/secondpage.php...this bit not working..secondpage.php code below..

the php file on the url, takes the coordinates and then if the "current location" button is pressed, displays the location of the device using google maps.
the device seems to be sending  the coordinates but  pushing the button on the site does not display the location....am wondering if i have totally misunderstood the HttpConnection,functionality,( see the sendHttpUrl method code..)

any guidance or help greatly appreciated...thanks in advance

package sendGPSurl;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.midlet.MIDlet;

public class SendPosURL extends MIDlet implements CommandListener {

    private Display display;
    private Form form;
    private Command exitCommand;
    private Command refreshCommand;
    private Command sendCommand;
    private Command sendCommand2;
    private StringItem text;

    private Location location;
    private LocationProvider locationProvider;
    private Coordinates coordinates;
    private Criteria criteria;
    private String smsString;

    /**
     * Constructor. Constructs the object and initializes displayables.
     */
    public SendPosURL() {
        form = new Form("BscH4 Project _Locator_Rory O'Flynn");
        sendCommand = new Command("Send Location by SMS",Command.OK,2);
       sendCommand2 = new Command("Send Location to Web",Command.OK,2);
        exitCommand = new Command("Exit", Command.EXIT, 3);
        refreshCommand = new Command("Position", Command.OK, 1);
        smsString = "; sending text from string2";
        text = new StringItem("Your position:", "\nSelect \"Position\"");
        form.append(text);

        form.addCommand(exitCommand);
        form.addCommand(refreshCommand);
        form.addCommand(sendCommand);
        form.addCommand(sendCommand2);
        form.setCommandListener(this);

        display = Display.getDisplay(this);
        display.setCurrent(form);

        criteria = new Criteria();
        criteria.setHorizontalAccuracy(500);
        // the following tells the Location API not to
        //incur costs when getting coordinates
       // criteria.setCostAllowed(false);


        try {
            locationProvider = LocationProvider.getInstance(criteria);

        } catch (LocationException e) {
            //TODO: Handle location exception.
            return;
        }

        if (locationProvider == null ) {
            text.setText("Location API failed.");
            form.removeCommand(refreshCommand);
        }
    }

        public void startApp() {
        // No implementation required.
    }

       public void pauseApp() {
        // No implementation required.
    }

        public void destroyApp(boolean unconditional) {
        // No implementation required
    }

   
    public void commandAction(Command c, Displayable d) {
        if (c == refreshCommand) {
            checkLocation();
        }
        else if (c == sendCommand) {
           sendSMS();
        }
        else if (c == sendCommand2) {
           sendToWeb();
        }
        else if (c == exitCommand) {
            notifyDestroyed();
        }
    }

    /**
     * Called to read current location.
     */
    private void checkLocation() {
        String string;
        try {
            location = locationProvider.getLocation(100);
        } catch (LocationException e) {
            //TODO: Handle exception.
            return;
        } catch (InterruptedException e) {
            //TODO: Handle exception.
            return;
        }

        coordinates = location.getQualifiedCoordinates();
        if (coordinates != null) {
            // Use coordinate information
            double lat = coordinates.getLatitude();
            double lon = coordinates.getLongitude();
            string = "http://blossompark.net/secondpage.php?q=" + lat + " " + lon;
        } else {
            string = "Calculating position, no fix yet.";
        }
        smsString = string;
        text.setText(string);

    }

    private void sendSMS(){
            Network n = new Network ();
        n.sendSms(smsString);

        text.setText("sending location to cellphone" + smsString);

    }
 private void sendToWeb(){
            Network w = new Network ();
        w.sendHttpUrl(smsString);

        text.setText("sending location to website" + smsString);

    }

}

//Network class begins here

package sendGPSurl;

import javax.microedition.io.*;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import java.io.*;

public class Network {
/**
   * Sends a sms message to a specific number.
   * @param number
   * @param message
   * @return
   */
  public Network ()
  {};

    //public boolean sendSms(String number, String message){
    //Rory changed method signature to below
    public boolean sendSms( String message){
    boolean result = true;
    try {
      //sets address to send message
     // String addr = "sms://"+number;
      //Rory change below to set number
        String addr = "sms://+353877525656";
      // opens connection
      MessageConnection conn = (MessageConnection) Connector.open(addr);
      // prepares text message
      TextMessage msg =
      (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
      //set text
      msg.setPayloadText(message);
      // send message
      conn.send(msg);
      } catch (Exception e) {
        result = false;
      }
      return result;
  }

    
  public void sendHttpUrl(String url){
     HttpConnection conn = null;
     try {
		conn = (HttpConnection) Connector.open( url );
      }
	catch (Throwable t){
        System.out.println("Exception occurred during GET "+t.toString());
		// handle the error here
	}
   
}
}
//secondpage.php code

<html>
	<head>
		<title>Second Page</title>
	</head>
	<body>
	<?php  
        // print_r($_GET);
         $coordinates = $_GET['q'];
         // echo " <br /> $coordinates ";
         ?>
          <br/>
          <a href="http://maps.google.com/maps?q=<?php echo $_GET['q'] ?> " > Current Location 
	
</body>
</html>

Open in new window

0
blossompark
Asked:
blossompark
  • 3
  • 3
1 Solution
 
HackneyCabCommented:
A few problems here (with the PHP side, I'm too rusty with Java at the moment to comment on that).

First of all, query string values cannot (or at least should not) have spaces in them, so where you have

string = "http://blossompark.net/secondpage.php?q=" + lat + " " + lon;

I recommend changing it to:

string = "http://blossompark.net/secondpage.php?qLat=" + lat + "&amp;qLon=" + lon;

That should give your PHP script at secondpage.php access to the following predefined variables:

$_GET['qLat']
$_GET['qLon']

And use var_dump($_GET) to see a nice explosion of everything inside the $_GET variable.

Also, it looks like (according to http://mapki.com/wiki/Google_Map_Parameters "Google Map Parameters") Google Maps requires a comma between the latitude and longitude, so try

<a href="http://maps.google.com/maps?q=<?php echo $_GET['qLat'].','.$_GET['qLon'] ?>">Current Location</a>

This assumes that your latitude and longitude are in decimal form (see that mapki.com page).
0
 
blossomparkAuthor Commented:
Hi HackneyCab,
                           thanks for  the response, had a look at mapki.com, will work with or without commas but to keep in with php best practise i guess commas are the wise choice, also easier to see than spaces, so will move to the comma option...
i have no experience of php so bear with me if the next question is a bit basic..
I've changed the code in secondpage.php a little bit (see below) and what i expect will happen is  the following;
a variable named $coordinates is created and stores whatever follows q= in the query string, which in this case will be: 53.290208540067006,-6.274996866083001
when "current Location" is pressed  the link to google maps is created and the contents of $coordinates is appended to give  the location on the map....however this is not happening...is there something wrong with  the syntax of "q=<?php echo $_GET[$coordinates]" or have i misunderstood how variables work here?
<html>
	<head>
		<title>Second Page</title>
	</head>
	<body>
	<?php  
               $coordinates = $_GET['q'];
                  ?>
          <br/>
          <a href="http://maps.google.com/maps?q=<?php echo $_GET[$coordinates] ?> " > Current Location 
	
</body>
</html>

Open in new window

0
 
HackneyCabCommented:
If you right-click on the "current Location" link and then "copy link location" (in Firefox, not sure whether there's an identical option in the menus for IE), can you paste the exact link here?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
blossomparkAuthor Commented:
0
 
HackneyCabCommented:
Sorry, I failed to spot that in the new version of your code you've stored the value in $coordinates, so you need to change the PHP block to this:

<?php echo $coordinates ?>

and then see whether that does the trick. If you're still getting a link which ends with the disappointing q= <blank> then it suggests that your Java program is not actually constructing the link to the secondpage.php request properly.

In theory, what you're doing is right, so keep hunting until you find where the trail has gone cold. (The fun is that you sometimes spot very tricky bugs in minutes, while other times a real simple will haunt you for days.)
0
 
blossomparkAuthor Commented:
changed <?php echo $_GET[$coordinates] to <?php echo $coordinates ?> and now behaves as i expect it to....great stuff!! thanks again!!
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now