• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HTTP connection thread problem to SERVLET on APACHE

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PROBLEM-The problem I'm facing is that I'm using a class in a midlet app for network purpose as separate thread (as per newer wtk). But the defect is that the thread doesn't return the data at first time and return second time only I assumed that the main midlet thread may be going too fast so I added join but after that the midlet even stopped working normal and get hangs if anybody wants i can post the source code also here. If have any alternative way please post it. And yes the class is sending a request to a servlet on apache server.

NOTE- If anybody finds the mistakes or any relevant detail missing please notify me

SOURCE CODE (MIDLET THREAD CLASS I M USING )
NETWORK.JAVA

/*
* Network.java
*/



import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

/**
* The Network object handles network connections
* to and from the server, the URL is stored in
* the descriptor file.
*/
public class Network extends java.lang.Object implements Runnable
{
private String URL = "http://localhost:8080/MSalesServlet";
private int port = 8080;
private String servlet = "/MSalesServlet";
private Controller controller;
//newely included
private int ch=0;
private StringItem upStatus;
private String tabrod=new String();
private String iden=new String();
private Vector v;
Thread t;


/** Creates new Network */
public Network(Controller controller)
{
this.controller = controller;
if(controller.getAppProperty("MSales-Server-URL") != null) URL = controller.getAppProperty("MSales-Server-URL");
if(controller.getAppProperty("MSales-Server-Port") != null) port = Integer.parseInt(controller.getAppProperty("MSales-Server-Port"));
if(controller.getAppProperty("MSales-Server-Servlet") != null) servlet = controller.getAppProperty("MSales-Server-Servlet");
}

public String getURL()
{
return(URL);
}

public Vector getData(String table, String identity)
{
return(getData(table, identity, null));
}

public Vector getData(String table,String identity,StringItem updateStatus)
{
try{
this.tabrod=table;
this.iden=identity;
this.upStatus=updateStatus;
this.ch=1;
t=new Thread(this);
t.start();
}catch(Exception e){System.out.println("Exceotption e "+e);}
return this.v;
}

public Vector getV()
{
return this.v;
}
public void sendNewOrders(String orders, StringItem updateStatus)
{
this.tabrod=orders;
this.upStatus=updateStatus;
this.ch=2;
t=new Thread(this);
t.start();
}

public void run()
{
try{
if(this.ch==1)
{
this.v=this.getData1(tabrod,iden,upStatus);
this.tabrod="";
this.iden="";
this.upStatus=null;
this.ch=0;
}
else if(this.ch==2)
{
this.sendNewOrders1(tabrod,upStatus);
this.tabrod="";
this.upStatus=null;
this.ch=0;
}
}catch(Exception e){}
}
public Vector getData1(String table, String identity, StringItem updateStatus)
{
Vector items = new Vector();
try
{

HttpConnection hc = null;
InputStream in = null;
OutputStream out = null;
try {
String message1 = "table="+table;
String message2="&id="+identity;
//String url = getAppProperty("PostMIDlet-URL");
//String url = getAppProperty("http:////192.168.1.5:8080//PostServlet");
System.out.println("The url is "+URL);
hc = (HttpConnection)Connector.open(URL);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length",Integer.toString(message1.length()));
out = hc.openOutputStream();
out.write(message1.getBytes());
hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length",Integer.toString(message2.length()));
out.write(message2.getBytes());
in = hc.openInputStream();
//int length = (int)hc.getLength();
//byte[] data = new byte[length];
//in.read(data);
StringBuffer result=new StringBuffer();
int currChar;
while ((currChar = in.read()) != -1)
{
if (currChar != '\n') result.append((char)currChar);
if (currChar == '\n')
{
items.addElement(result.toString());
result = new StringBuffer();
}
}
}
catch (IOException ioe) {
StringItem stringItem = new StringItem(null, ioe.toString());
}
finally {
try {
if (out != null) out.close();
if (in != null) in.close();
if (hc != null) hc.close();
}
catch (IOException ioe) {}
}

/*StringBuffer result = new StringBuffer(); --- old code --------------
if(updateStatus != null) updateStatus.setText("Finding Server...");
HttpConnection c = (HttpConnection)Connector.open(URL+":"+port+servlet+"?table="+table+"&id="+identity);
if(updateStatus != null) updateStatus.setText("Connected...");
InputStreamReader isr = new InputStreamReader(c.openInputStream());
int currChar;
if(updateStatus != null) updateStatus.setText("Transferring...");
while ((currChar = isr.read()) != -1)
{
if (currChar != '\n') result.append((char)currChar);
if (currChar == '\n')
{
items.addElement(result.toString());
result = new StringBuffer();
}
}
isr.close();
c.close();*/
}
catch(Exception e) { System.out.println("Network Exception: "+e.toString()); }
return(items);
}

public void sendNewOrders1(String orders, StringItem updateStatus)
{
HttpConnection c;
OutputStream os;
try
{
StringBuffer result = new StringBuffer();
if(updateStatus != null) updateStatus.setText("Finding Server...");
c = (HttpConnection)Connector.open(URL+":"+port+servlet+"?table=OrderUpload");
if(updateStatus != null) updateStatus.setText("Connected...");
c.setRequestMethod(HttpConnection.POST);
os = c.openOutputStream();
// send request to the servlet
byte msg[] = orders.getBytes();
if(updateStatus != null) updateStatus.setText("Transferring...");
for(int i=0; i<msg.length; i++) {
os.write(msg[i]);
}
// check the response from the server
if (c.getResponseCode() != HttpConnection.HTTP_OK)
System.out.println("HTTP Post Error");
// close all the connections/streams
if(os != null) os.close();
if(c != null) c.close();
}
catch(Exception e)
{
System.out.println("Network Exception: "+e.toString());
e.printStackTrace();
}
}
}

 
Rituraj tyagi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody
I m posting the solution myself cause I found it some crooked thing lets get to work straight

SOLUTION-My example I was facing the prob above mentioned now for that I conclude that every midlet needs an permission for using on air connection and in my case I need to get a login response so whenever it asked me for permission till that moment it become the midlet jumps the test condition and show login error so better do one thing I tried is first I put a form saying initializing connection (its a malprcatice I think according to industry standard ) and sending some garbage connection which will be rejected obviously but enable other network connections after that to easily work without any interruption and midlet goes well.

CONCLUSION-At first any midlet trying to using HTTP connection needs that permission first and till the moment it gets it may be too late(due to might be some thread functioning )
so always initialize connection with some useless connection request.
 
reply
    Bookmark Topic Watch Topic
  • New Topic