Geo*_*zen 0 java json blackberry http blackberry-eclipse-plugin
如何从作为客户端的黑莓应用程序向服务器发送JSON请求以从服务器获取信息以在BB应用程序中使用它我在Windows 7下使用blackberry eclipse
我试试这段代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我通过线程中的run方法调用此方法
当模拟器到达(rc = c.getResponseCode();)时,运行代码停止
我调试代码,当它出现此错误时它停止
本地连接在~120000之后超时
任何帮助
在模拟器中运行应用程序时,请确保在Eclipse的"运行配置"或"调试配置" - >"模拟器选项卡" - >"常规选项卡"中使用模拟器启用了启动移动数据系统连接服务(MDS-CS).
如果未启用,则应查看本指南" 使用BlackBerry Smartphone Simulator 测试BlackBerry设备应用程序 ",尤其是" 测试使用HTTP连接的BlackBerry设备应用程序 "部分.总而言之,您必须启用MDS-CS.启用后,您应该重新启动模拟器.以下是本指南的引用:
启动BlackBerry Smartphone Simulator时启动BlackBerry MDS Connection Service
- 在Eclipse®的"运行"菜单上,单击"调试配置"或"运行配置".
- 展开BlackBerry Simulator项目.
- 完成以下任务之一:
- 要使用现有的启动配置,请在BlackBerry Simulator下单击启动配置.
- 要创建新的启动配置,请右键单击BlackBerry Simulator,然后选择"新建".
- 单击"模拟器"选项卡.
- 单击"常规"选项卡.
- 选中使用模拟器启动移动数据系统连接服务(MDS-CS)复选框.
- 单击"应用"
编辑:
或者,使用模拟器时,您可以将;deviceside=true后缀添加到传递给的URL Connector.open().通过设置deviceside = true,您可以指定应直接从手持设备(在您的情况下为模拟器)打开底层TCP连接,因此不会使用BlackBerry MDS Connection Service.以下是基于您的代码的代码段:
if (DeviceInfo.isSimulator()) {
urlPath += ";deviceside=true";
} else {
urlPath += connectionDependentSuffix; // suffix that is relevant to
// the desired connection option
}
c = (HttpConnection)Connector.open(urlPath);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
3241 次 |
| 最近记录: |