I am working on blackberry eclipse. I need to get an session ID after sending an HTTP request and I have to parse that data through JSON. Can anyone tell what is wrong with this code?
private void l()throws IOException {
// TODO Auto-generated method stub
//Dialog.alert("click continue");
HttpConnection hconn = null;
InputStream os = null;
String total ="";
String URL = "http://www.abc.com";
URLEncodedPostData data = null;
try {
System.out.println("----------------------START------------------------------");
hconn = (HttpConnection)Connector.open(URL);
hconn.setRequestMethod(HttpConnection.POST);
hconn.setRequestProperty("Content - Type", "application/json;charset=UTF-8");
//hconn.setRequestProperty("method" , "system.connect");
System.out.println(">>>>>>>>>>>>1"+hconn);
URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
data = new URLEncodedPostData("UTF-8", false);
data.append("method", "system.connectmethod");
URL = URL + "?" + data.toString();
OutputStream strmOut = hconn.openOutputStream();
strmOut.write(oPostData.getBytes());
strmOut.flush();
//strmOut.close();
System.out.println(strmOut);
System.out.println(">>>>>>>>>>>>>>>>>>>DATA SENT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
int resp = hconn.getResponseCode();
if (resp == HttpConnection.HTTP_OK)
{
char size = 1024;
char[] buffer = new char[size];
int len;
InputStreamReader isr = new InputStreamReader(hconn.openInputStream(), "UTF-8");
StringBuffer sbuf = new StringBuffer();
while ((len = isr.read(buffer))>0)
{
sbuf.append(buffer);
total+= len;
}
String id = total;
System.out.println(">>>>>>>>>>>>>>>>>>>RESPONSE CODE RECIEVED>>>>>>>>>>>>>>"+resp);
System.out.println(">>>>>>>>>>>>>>>>>>>RECIEVED DATA>>>>>>"+total);
System.out.println(">>>>>>>>>>>>>>>>>>>recieved>>>>>>>"+len);
System.out.println(">>>>>>>>>>>bufferdata>>>>>>>>>>>>>>>>>");
System.out.println(sbuf);
System.out.println(">>>>>>>>>>>>>Inputstream>>>>>>"+isr);
JSONObject object = new JSONObject();/*What value should i pass in it ,as it shows error in passing "total" */
//object.put("SeesionId","total1");
object = object.getJSONObject("#data");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+object);
isr.close();
strmOut.close();}}
catch (Exception e)
{
}
} }