File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes Space character in URL error when running midlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Space character in URL error when running midlet" Watch "Space character in URL error when running midlet" New topic
Author

Space character in URL error when running midlet

sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
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");

}
}

}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

When you post, there is a "CODE" tag that you can use so that your posted code still keeps its formatting, like indentation. Posting code like you did makes it very difficult to read and makes that even more difficult for us to help you.
There are Buttons under the "Add Reply" button that can help.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
MIDLET CODE
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class FormPizzaexp extends Form implements CommandListener {

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){
try{
submitOrder();
}
catch (Exception e){
System.out.println(e.toString());
}


}

}

private void submitOrder () throws IOException {
HttpConnection = null;
InputStream iStrm = null;
String url = "http://localhost:8000/pizzaexporder/pizzaexporderservlet"+ "pizzaexporder=" + pizzaexporder;

boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);

for (int i=0; i<pizzaexpMenu.size(); i++)
if (selected[i] == true)
String pizzaexporder = pizzaexpMenu.getString(i);


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 {
if (iStrm!=null)
iStrm.close();
if (http != null)
http.close();
}

}
}
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");

}
}

}
sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
MIDLET CODE
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class FormPizzaexp extends Form implements CommandListener {

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){
try{
submitOrder();
}
catch (Exception e){
System.out.println(e.toString());
}


}

}

private void submitOrder () throws IOException {
HttpConnection = null;
InputStream iStrm = null;
String url = "http://localhost:8000/pizzaexporder/pizzaexporderservlet"+ "pizzaexporder=" + pizzaexporder;

boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);

for (int i=0; i<pizzaexpMenu.size(); i++)
if (selected[i] == true)
String pizzaexporder = pizzaexpMenu.getString(i);


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 {
if (iStrm!=null)
iStrm.close();
if (http != null)
http.close();
}

}
}
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");

}
}

}
sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
Apologies for posting my code twice is there anyway I can remove both posts.
kok seong khew
Greenhorn

Joined: Jul 04, 2003
Posts: 14
Sorry, just want to test on the CODE tag as mentioned...
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by sachin sav:
Apologies for posting my code twice is there anyway I can remove both posts.
Yes, there is. Just click on the paper-and-pen button on top of the post you want to remove, check the "Delete post" checkbox, and click the "Edit post" form submit button.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
David Price
Ranch Hand

Joined: Jan 22, 2003
Posts: 93
By the way, I think I may see why there's a space in your URLs. See this line:

And 'pizza' is one of the entries in your ChoiceGroup, e.g. "Margherita �4.95". So your URL will be:
"http://localhost:8000/pizzaexporder/PizzaExpOrderServletpizzaexporder=Margherita �4.95"
I.e., the space is before the price. Also, I expect you meant to include a "?" between the servlet name and the query parameter name. And you've still got a problem in that some pizza names include spaces. You could use code like this:

To really do it right, you should 'URL encode' the pizza name. Have a look at the JavaDocs for J2SE class java.net.URLEncoder, then implement a small method to do the same thing (since that class is not part of MIDP).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Space character in URL error when running midlet