发送从黑莓应用程序JSON请求 [英] Send JSON Request from blackberry application
问题描述
怎么能发送从客户作为工作到服务器,从服务器获取信息的BB应用程序中使用他们的BlackBerry应用程序JSON请求
我使用黑莓日食Windows下7
我尝试这个code
公共无效loginRequest()抛出IOException异常,JSONException {
的HttpConnection c = NULL;
InputStream为= NULL;
INT RC; JSONObject的postObject =新的JSONObject();
postObject.put(办法,法);
//postObject.put(\"params参数); 尝试{
C =(HttpConnection的)Connector.open(urlPath); //设置请求方法和头
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty(内容类型,应用/ JSON的;字符集= UTF-8);
c.setRequestProperty(内容长度,+(postObject.toString()长度() - 2));
c.setRequestProperty(法,GET); //得到响应code将打开连接,
//发送请求,并读取HTTP响应头。
//该头被存储到请求。
RC = c.getResponse code();
如果(RC!= HttpConnection.HTTP_OK){
抛出新IOException异常(HTTP响应code:+ RC);
} 是= c.openInputStream(); //获取长度和处理数据
INT LEN =(int)的c.getLength();
如果(LEN大于0){
INT实际= 0;
INT读取动作= 0;
字节[]数据=新的字节[LEN]
而((读取动作= LEN)及!及(实际= -1)!){
实际= is.read(数据读取动作,LEN - 读取动作);
读取动作+ =实际;
}
//获取JSON字符串
的System.out.println(新的String(数据));
}
其他{
INT CH;
而((CH = is.read())!= - 1){
//去做
/ *
处理((字节)CH);
* /
}
}
}赶上(抛出ClassCastException E){
抛出新抛出:IllegalArgumentException(不是一个HTTP URL);
} {最后
如果(是!= NULL)
is.close();
如果(C!= NULL)
c.close();
}
}
我通过调用run方法这个方法在一个线程
在模拟器距离( RC = c.getResponse code(); )运行code停止
我调试code,当它达到这个错误这句话停止
本地连接超时后〜120000
任何帮助
当运行在模拟器的应用程序,确保将仿真中启用的启动移动数据系统连接服务(MDS-CS) Eclipse的运行配置或调试配置 - >选项卡模拟器 - >常规选项卡。
如果没有启用它,你应该检查该指南<一个href=\"http://docs.blackberry.com/en/developers/deliverables/35144/Testing_a_BB_app_with_BB_smartphone_sim_1889924_11.jsp\">Testing与BlackBerry设备应用程序的BlackBerry智能手机模拟器,特别是
当您启动BlackBerry智能手机模拟器启动BlackBerry MDS Connection Service的
- 在Eclipse®,运行菜单上,单击调试配置或运行配置。
- 展开BlackBerry模拟器项目。
- 的完成以下任务之一:结果
- 要与现有的启动配置,在BlackBerry模拟器工作,单击启动配置。
- 要创建一个新的启动配置中,右键单击BlackBerry模拟器,选择新建。
- 单击Simulator选项卡。
- 单击常规选项卡。
- 选择与仿真复选框推出移动数据系统连接服务(MDS-CS)。
- 单击Apply(应用)。
块引用>修改:结果
另外,使用模拟器时,你可以添加; deviceside = TRUE
后缀以传递给的URL Connector.open()
。通过设置 deviceside = TRUE 您指定的TCP连接应直接从手持设备(模拟你的情况)打开,因此黑莓MDS连接服务将无法使用。这是根据你的code一个code片断:如果(DeviceInfo.isSimulator()){
urlPath + =; deviceside =真正的;
}其他{
urlPath + = connectionDependentSuffix; //后缀,是有关
//所需的连接选项
}
C =(HttpConnection的)Connector.open(urlPath);希望这有助于。
how can i send JSON request from blackberry application that works as client to the server to get an information from the server to use them in BB application i use blackberry eclipse under windows 7
i try this code
public void loginRequest() throws IOException, JSONException{ HttpConnection c = null; InputStream is = null; int rc; JSONObject postObject = new JSONObject(); postObject.put("method", method); //postObject.put("params", Parameters); try{ c = (HttpConnection)Connector.open(urlPath); // Set the request method and headers c.setRequestMethod(HttpConnection.GET); c.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); c.setRequestProperty("Content-Length", "" + (postObject.toString().length() - 2)); c.setRequestProperty("method", "GET"); // Getting the response code will open the connection, // send the request, and read the HTTP response headers. // The headers are stored until requested. rc = c.getResponseCode(); if (rc != HttpConnection.HTTP_OK){ throw new IOException("HTTP response code: " + rc); } is = c.openInputStream(); // Get the length and process the data int len = (int)c.getLength(); if (len > 0){ int actual = 0; int bytesread = 0 ; byte[] data = new byte[len]; while ((bytesread != len) && (actual != -1)){ actual = is.read(data, bytesread, len - bytesread); bytesread += actual; } //Get the JSON String System.out.println(new String(data)); } else{ int ch; while ((ch = is.read()) != -1){ //TODO /* process((byte)ch); */ } } }catch (ClassCastException e){ throw new IllegalArgumentException("Not an HTTP URL"); }finally { if (is != null) is.close(); if (c != null) c.close(); } }
i call this method by run method in a thread
when the simulator reach (rc = c.getResponseCode();) running code stops
i debug the code and it stops when it reach this statement with this error
Local connection timed out after ~ 120000
any help
解决方案When running the application in simulator, make sure you enabled the Launch Mobile Data System Connection Service (MDS-CS) with simulator in Eclipse's "Run Configurations" or "Debug Configurations"->"Simulator tab"->"General tab".
If it is not enabled, you should check this guide "Testing a BlackBerry device application with the BlackBerry Smartphone Simulator", particularly the "Testing a BlackBerry device application that uses an HTTP connection" section. To make a long story short, you have to enable the MDS-CS. After enabling it, you should restart your simulator. Here is a quote from this guide:
Start the BlackBerry MDS Connection Service when you start the BlackBerry Smartphone Simulator
- In Eclipse®, on the Run menu, click Debug Configurations or Run Configurations.
- Expand the BlackBerry Simulator item.
- Complete one of the following tasks:
- To work with an existing launch configuration, under BlackBerry Simulator, click a launch configuration.
- To create a new launch configuration, right-click BlackBerry Simulator, select New.
- Click the Simulator tab.
- Click the General tab.
- Select the Launch Mobile Data System Connection Service (MDS-CS) with simulator check box.
- Click Apply.
Edit:
Alternatively, when using a simulator you can add;deviceside=true
suffix to the url that you pass toConnector.open()
. By setting deviceside=true you specify that the underlying TCP connection should be opened directly from the handheld (simulator in your case), therefore BlackBerry MDS Connection Service will not be used. Here is a code snippet based on your code:if (DeviceInfo.isSimulator()) { urlPath += ";deviceside=true"; } else { urlPath += connectionDependentSuffix; // suffix that is relevant to // the desired connection option } c = (HttpConnection)Connector.open(urlPath);
Hope this helps.
这篇关于发送从黑莓应用程序JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!