We are developing a chat application for Rel. CDMA phone and for this we are trying to send an receive data but its not working when we are using POST method. Can any one explain me how to write code it for server n client side, bcoz we r written server for the same.
public String readData(String url)
{
if(url==null)
return null;
String data=null;
try
{
if (!url.startsWith("http://") &&
!url.startsWith("https://"))
{
url = "http://" + url;
}
} catch (Exception ex)
{
return null;
}
try
{
buffer=new StringBuffer();
conn = (HttpConnection)Connector.open(url);
//// conn.setRequestMethod(HttpConnection.GET);
conn.setRequestMethod(HttpConnection.POST);
//// conn.setRequestProperty("If-Modified-Since","29 Oct 1999 19:43:31 GMT");
conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//post
DataOutputStream os = conn.openDataOutputStream();
os.writeInt(20);
os.writeInt(1);
os.writeInt(12);
os.writeUTF("919820301007");
os.writeUTF("919322078009");
// os.write(postData.getBytes());
////// os.flush(); // Optional, openInputStream will flush
//-post
int responseCode=conn.getResponseCode();
System.out.println("responseCode="+responseCode);
if(responseCode== HttpConnection.HTTP_OK && ! canceled)
{
int len = (int) conn.getLength();
is = conn.openInputStream();
if (len == -1)
{
// unknown length?
// read until .read() returns -1
int ch;
while((ch=is.read())!= -1)
{
buffer.append((char) ch);
}
}
else
{
// known length
for (int i=0; i <len; i++)//="i++)"
{
//// System.out.println("&&&&&&&&&& : "+ is.read());
buffer.append((char) is.read());
}
////// int ch;
////// while((ch=is.read())!= -1)
////// {
////// System.out.println("&&&&&&&&&& : "+(char) ch);
////// buffer.append((char) ch);
////// }
}
//// is.close();
data=buffer.toString();
}
else
{
}
}
catch(Exception ex) {
//// System.out.println("Exception=="+ex);
ex.printStackTrace();
return null;
}
finally
{
try{
if(is!=null){
is.close();
is=null;
}
}catch (Exception ex){}
try{
if(conn!=null){
conn.close();
conn=null;
}
}catch (Exception ex){}
}
System.out.println("returning data=="+data);
if(!canceled)
return data;
else
return null;
}