I have programmed a midlet to send information via a servlet. However I get the following error java.lang.IllegalArgumentException: Space character in URL.
I understand that the error says what it means and that there must be a space in the URL. However I have looked at the URL but see no spaces. Could this error be refering to something else I include my midlet and servlet code below. I have been looking at this for quite a while but cannot see what is wrong. I hope somebody can help.
MIDLET CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class FormPizzaexp extends Form implements CommandListener, Runnable {
private ChoiceGroup pizzaexpMenu;
private MainScreen midlet;
private Command cmdBack;
private Command cmdSelect;
public FormPizzaexp (String title, MainScreen midlet ){
super (title);
this.midlet = midlet;
cmdBack = new Command("Back", Command.BACK, 1);
cmdSelect = new Command("Select",Command.OK, 2);
pizzaexpMenu = new ChoiceGroup ("What would you like to Order?", Choice.EXCLUSIVE,
new String [] {"ANTIPIZZE", "Garlic Bread �1.95", "Bruschetta �3.25", "Baked Dough Balls �1.95", "SALADS AND VARIATIONS", "Mozzarella and Tomato Salad �6.45", "Lasagne Pasticcate �7.10", "Tortellini �7.10", "PIZZE", "Margherita �4.95", "American Hot �7.70", "Soho Pizza �6.95", "Sloppy Giussepe �7.15", "WINE", "House White Bottle 75cl �10.95", "Half House White Bottle 37.5cl �5.95", "House Red Bottle 75cl �10.95", "Half House Red Bottle 37.5cl �5.95", "SOFT DRINKS", "Coke �1.65", "Sprite �1.65", "Mineral Water �1.00", "Apple Juice �1.65", "DESSERTS", "Choclate Fudge Cake �3.85", "Tiramisu �3.85", "Vanilla Ice Cream �2.10"}, null);
append(pizzaexpMenu);
addCommand(cmdBack);
addCommand(cmdSelect);
setCommandListener(this);
}
public void commandAction(Command c, Displayable s) {
if (c == cmdBack)
midlet.displayMainForm ();
else if (c == cmdSelect){
Thread t = new Thread(this);
t.start();
}
}
public void run(){
submitOrder();
}
private void submitOrder () {
HttpConnection http = null;
InputStream iStrm = null;
String pizza = null;
boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);
for (int i=0; i<pizzaexpMenu.size(); i++)
if (selected[i] == true)
pizza = pizzaexpMenu.getString(i);
String url ="http://localhost:8000/pizzaexporder/PizzaExpOrderServlet"+"pizzaexporder="+pizza;
try {
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
if (http.getResponseCode() == HttpConnection.HTTP_OK){
iStrm = http.openInputStream();
int length =(int) http.getLength();
if (length>0){
byte servletData[] = new byte [length];
iStrm.read(servletData);
append("You passed to the servlet" + new String(servletData));
}
else
append("Unable to read the data");
}
}
catch (Exception e){
append("Network error");
System.out.println(e.toString());
}finally {
try {
if (iStrm!=null){
iStrm.close();
}
if (http != null){
http.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
SERVLET CODE
import java .io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class PizzaExpOrderServlet extends HttpServlet {
private Statement stmt = null;
private Connection conn = null;
private String URL ="jdbc
dbc
izzaExpOrder";
public void init (ServletConfig config)
throws ServletException {
super.init(config);
try {
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(URL, "", "");
}
catch (Exception e) {
e.printStackTrace () ;
conn = null;
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pizzaexporder = request.getParameter("pizzaexporder");
if (pizzaexporder == null)
{
response.sendError(response.SC_BAD_REQUEST, "Unable to read parameters");
return;
}
PrintWriter output = response.getWriter();
response.setContentType ("text/html");
boolean success = insertIntoDB ("'"+ "',' ',' ','" + pizzaexporder +"',' ','"+ "'");
if (success)
output.print ("<H2>Thank You For Ordering Your Customer Reference is:</H2>");
else
output.print ("<H2> An error ocurred</H2>");
output.close();
}
private boolean insertIntoDB (String stringtoinsert){
try{
stmt = conn.createStatement ();
stmt.execute("INSERT INTO PizzaExpOrder values (" + stringtoinsert + ");");
stmt.close();
}
catch (Exception e) {
System.err.println ("Error: Problems with adding new entry");
e.printStackTrace();
return false;
}
return true;
}
public void destry ()
{
try {
conn.close();
}
catch (Exception e) {
System.err.println ("Problem closing the database");
}
}
}