09-26-2011 07:23 AM
Hello,
Thanks!!
I am using REST type responce of webservice.
I am using HTTP Connection for connecting.
I am create the connection but not get the responce .
If you have any sample application about to get Responce from webService and json paresing.
Please Help...AEAP
Thanks!!
Ani
09-28-2011 05:58 AM
Hello,
Please find the above code i am done the http connection get the result from webservice.
when i am pass the result in method parseJSONResponceInBB ().
package mypackage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
public class HttpCon
{
public static final int CONNECTION_DEFAULT = 0;
public static final int CONNECTION_BIS = 1;
public static final int CONNECTION_BES = 2;
public static final int CONNECTION_TCPIP = 3;
public static final int CONNECTION_WIFI = 4;
public static final int CONNECTION_WAP=5;
Bitmap bit;
String aConnctionUrl="";
MyApp _App;
String result;
JSONObject jsobj;
public void HttpCon()
{
// url = url+aconn;
_App = (MyApp) UiApplication.getUiApplication();
}
public void CreateUrl()
{
int connType = getCoverageBasedConnectionType();
aConnctionUrl = Makeurl(connType);
// aConnctionUrl = ";deviceside=true";// simuulator
}
public String IssueHTTPPostL(String url)
{
StringBuffer sb = new StringBuffer();
synchronized(this)
{
HttpConnection httpConn = null;
int connType = getCoverageBasedConnectionType();
String aconn = Makeurl(connType);
url = url+aconn;
HttpCon httpclient = new HttpCon();
// Dialog.alert(url);
InputStream is = null;
// InputStream is1 = null;
OutputStream os = null;
// Dialog.alert(url);
try {
// StreamConnection s = null;
httpConn = (HttpConnection)Connector.open(url);//url
// Setup HTTP Request to POST
httpConn.setRequestMethod(HttpConnection.POST);
/**Caution: os.flush() is controversial. It may create unexpected behavior
on certain mobile devices. Try it out for your mobile device **/
int i = httpConn.getResponseCode();
// Dialog.alert("Responce is :"+is);
// new XmlDemoMainScreen(is);//
if (i == HttpConnection.HTTP_OK)
{
Dialog.alert("Http ok");
System.out.print("HTTP_OK");
}
is = httpConn.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = is.read(responseData))) {
rawResponse.append(new String(responseData, 0, length));
}
result = rawResponse.toString();
Dialog.alert(result);
if(result != null)
{
parseJSONResponceInBB(result);
}
// System.out.println(result);
} catch (IOException e)
{
Dialog.alert("IOException"+e.toString());
} finally {
if(is!= null)
try {
is.close();
if(os != null)
os.close();
if(httpConn!=null)
httpConn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
// Dialog.alert(result);
// return result;
}
public int getCoverageBasedConnectionType()
{
if (CoverageInfo.isCoverageSufficient(CoverageInfo.CO VERAGE_DIRECT,
RadioInfo.WAF_WLAN, false))
{
// _app.LogWrite("WAF_WLAN");
//MSM.LogWrite("makeHttpConnection WAF_WLAN");
return CONNECTION_WIFI;
}
if (CoverageInfo.isCoverageSufficient(CoverageInfo.CO VERAGE_DIRECT))
{
// _app.LogWrite("CONNECTION_WAP");
// MSM.LogWrite("makeHttpConnection CONNECTION_WAP");
return CONNECTION_WAP;
}
if (CoverageInfo.isCoverageSufficient(CoverageInfo.CO VERAGE_BIS_B)) {
// _app.LogWrite("CONNECTION_BIS");
return CONNECTION_BIS;
}
if (CoverageInfo.isCoverageSufficient(CoverageInfo.CO VERAGE_MDS)) {
// _app.LogWrite("CONNECTION_BES");
return CONNECTION_BES;
}
// _app.LogWrite("CONNECTION_DEFAULT");
return CONNECTION_DEFAULT;
}
public String Makeurl(int connType)
{
// LogWrite("Makeurl"+connType);
String url = "";
if (url.indexOf(";deviceside=") == -1) {
// System.out.println("makeHttpConnection inside If: ");
switch(connType){
case CONNECTION_DEFAULT:
url = url + ";deviceside=false";
// System.out.println("CONNECTION_DEFAULT url: " + url);
break;
case CONNECTION_BES:
url = url + ";deviceside=false";
break;
case CONNECTION_BIS:
url = url + ";XXXXXXXXXXXXXXXX";
break;
case CONNECTION_TCPIP:
url = url + ";deviceside=true";
// System.out.println("CONNECTION_TCPIP url: " + url);
break;
case CONNECTION_WIFI:
url = url + ";interface=wifi";
// System.out.println("CONNECTION_WIFI url: " + url);
break;
case CONNECTION_WAP:{
ServiceBook sb = ServiceBook.getSB();
net.rim.device.api.servicebook.ServiceRecord[] records = sb.findRecordsByCid("WPTCP");
String uid = null;
/* System.out.println("*****************Records are :" + records);
System.out.println("*****************Records length :" + records.length); */
for(int i=0; i < records.length; i++) {
if (records[i].isValid() && !records[i].isDisabled()) {
if (records[i].getUid() != null && records[i].getUid().length() != 0) {
if ((records[i].getCid().toLowerCase().indexOf("wptcp ") != -1) &&
records[i].getUid().toLowerCase().indexOf("wap2") !=- 1 &&
(records[i].getUid().toLowerCase().indexOf("wifi") == -1) &&
(records[i].getUid().toLowerCase().indexOf("mms") == -1)) {
uid = records[i].getUid();
break;
}
}
}
}
if (uid != null) {
url = url +";ConnectionUID=" + uid;
}
if(uid==null) {
StringBuffer URLString = new StringBuffer();
URLString.append(url);
records =sb.findRecordsByCid("WAP");
System.out.println("*****************Records are :" + records);
System.out.println("*****************Records length :" + records.length);
if(records.length>0)
for(int i = 0; i < records.length; i++){
if(records[i].getName().equalsIgnoreCase("WAP Transport")){
URLString.append(";WAPGatewayIP=");
String networkAddress =records[i].getNetworkAddress();
URLString.append(networkAddress.substring(0,networ kAddress.indexOf(':')));
URLString.append(";WAPGatewayAPN=");
URLString.append(records[i].getAPN());
url = URLString.toString();
System.out.println("URL="+url);
}
}
else
{
url = url + ";deviceside=true";
}
}
}
}
}
return url;
}
void parseJSONResponceInBB(String jsonInStrFormat){
try {
Dialog.alert("parser Method");
JSONObject json = new JSONObject(jsonInStrFormat);
JSONArray jArray= json.getJSONArray("Metro");
//this for loop iterates two times as it contains two json object
for(int i=0;i<jArray.length();i++){
Dialog.alert("for loop aft");
JSONObject j = jArray.getJSONObject(i);
String from = j.optString("metro_city_id");
Dialog.alert("Id :"+from);
String to =j.optString("metro_city_name");
Dialog.alert("Name :"+to);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
time the value of result is null.
03-20-2013 08:19 AM