[英]Send JSON Request from blackberry application
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 我如何从充当客户端的黑莓应用程序向服务器发送JSON请求以从服务器获取信息以在BB应用程序中使用它们,我在Windows 7下使用黑莓Eclipse
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 我在线程中通过run方法调用此方法
when the simulator reach ( rc = c.getResponseCode(); ) running code stops
当模拟器到达( rc = c.getResponseCode(); )时,运行代码停止
i debug the code and it stops when it reach this statement with this error 我调试代码,并在到达此语句并出现此错误时停止
Local connection timed out after ~ 120000
本地连接在〜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". 在模拟器中运行应用程序时,请确保在Eclipse的“运行配置”或“调试配置”->“模拟器选项卡”->“常规选项卡”中启用了带有模拟器的启动移动数据系统连接服务(MDS-CS) 。
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. 如果未启用,则应检查本指南“ 使用BlackBerry Smartphone Simulator 测试BlackBerry设备应用程序 ”,尤其是“ 使用HTTP连接测试BlackBerry设备应用程序 ”部分。 To make a long story short, you have to enable the MDS-CS.
简而言之,您必须启用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
启动BlackBerry Smartphone Simulator时,启动BlackBerry MDS Connection Service。
- In Eclipse®, on the Run menu, click Debug Configurations or Run Configurations.
在Eclipse®中的“运行”菜单上,单击“调试配置”或“运行配置”。
- Expand the BlackBerry Simulator item.
展开BlackBerry Simulator项目。
- Complete one of the following tasks:
完成以下任务之一:
- To work with an existing launch configuration, under BlackBerry Simulator, click a launch configuration.
要使用现有的启动配置,请在BlackBerry Simulator下单击启动配置。
- To create a new launch configuration, right-click BlackBerry Simulator, select New.
要创建新的启动配置,请右键单击BlackBerry Simulator,然后选择“新建”。
- Click the Simulator tab.
单击模拟器选项卡。
- Click the General tab.
单击常规选项卡。
- Select the Launch Mobile Data System Connection Service (MDS-CS) with simulator check box.
选中使用模拟器启动移动数据系统连接服务(MDS-CS)复选框。
- Click Apply.
单击应用。
Edit : 编辑 :
Alternatively, when using a simulator you can add ;deviceside=true
suffix to the url that you pass to Connector.open()
. 或者,使用模拟器时,可以在传递给
Connector.open()
的URL中添加;deviceside=true
后缀。 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. 通过设置deviceside = true,您指定应直接从手持设备(在您的情况下为模拟器)打开基础TCP连接,因此将不使用BlackBerry MDS Connection Service。 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. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.