Blackberry 5.0中的Json解析
我正在黑莓版本5.0中开发应用程序,我已经导入了5.0中json所需的所有库 我已经从这个url下载了这个库 即使我没有得到回应,我在这段代码中遗漏了什么请帮助我 下面是我的json解析代码Blackberry 5.0中的Json解析,blackberry,Blackberry,我正在黑莓版本5.0中开发应用程序,我已经导入了5.0中json所需的所有库 我已经从这个url下载了这个库 即使我没有得到回应,我在这段代码中遗漏了什么请帮助我 下面是我的json解析代码 package mypackage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.microedition.io.Connecto
package mypackage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import JSON_ME_Library.src.org.json.me.JSONArray;
import JSON_ME_Library.src.org.json.me.JSONException;
import JSON_ME_Library.src.org.json.me.JSONObject;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen
{
String url="http://www.appymail.com/iphone-messenger/456842/";
public MyScreen()
{
setTitle("Json Parsing Sample");
String aa=jsonresponse(url);
if(aa.equalsIgnoreCase(""))
{
add(new LabelField("NO res"));
}
else
{
parseJSONResponceInBB(aa);
}
}
void parseJSONResponceInBB(String jsonInStrFormat)
{
try {
JSONObject json = new JSONObject(jsonInStrFormat);
JSONArray jArray= json.getJSONArray("messages");
//JSONArray arr=jArray.getJSONArray(0);
for(int i=0;i<jArray.length();i++)
{
JSONObject j = jArray.getJSONObject(i);
String from = j.getString("id");
add(new LabelField("id=="+from));
String to =j.getString("title");
add(new LabelField("title=="+to));
String message=j.getString("body");
add(new LabelField("Body=="+message));
}
} catch (JSONException e)
{
e.printStackTrace();
}
}
public static String jsonresponse (String url)
{
String response = null;
HttpConnection httpConnection = null;
InputStream inStream = null;
int code;
StringBuffer stringBuffer = new StringBuffer();
try {
httpConnection = (HttpConnection) Connector.open(url, Connector.READ);
httpConnection.setRequestMethod(HttpConnection.GET);
code = httpConnection.getResponseCode();
if(code == HttpConnection.HTTP_OK)
{
inStream=httpConnection.openInputStream();
int c;
while((c=inStream.read())!=-1)
{
stringBuffer.append((char)c);
}
response=stringBuffer.toString();
System.out.println("Response Getting from Server is ================" + response);
}
else
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.inform("Connection error");
}
});
}
}
catch (Exception e)
{
System.out.println("caught exception in jsonResponse method"+e.getMessage());
}
finally
{
// if (outputStream != null)
// {
// outputStream.close();
// }
if (inStream != null)
{
try {
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (httpConnection != null )
{
try {
httpConnection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return response;
}
}
package-mypackage;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入javax.microedition.io.Connector;
导入javax.microedition.io.HttpConnection;
导入JSON_ME_Library.src.org.JSON.ME.JSONArray;
导入JSON_ME_Library.src.org.JSON.ME.JSONException;
导入JSON_ME_Library.src.org.JSON.ME.JSONObject;
导入net.rim.device.api.ui.ui应用程序;
导入net.rim.device.api.ui.component.ButtonField;
导入net.rim.device.api.ui.component.Dialog;
导入net.rim.device.api.ui.component.LabelField;
导入net.rim.device.api.ui.container.main屏幕;
公共最终类MyScreen扩展主屏幕
{
字符串url=”http://www.appymail.com/iphone-messenger/456842/";
公共MyScreen()
{
setTitle(“Json解析示例”);
字符串aa=jsonresponse(url);
if(aa.相等信号情况(“”)
{
添加(新LabelField(“无res”);
}
其他的
{
parseJSONResponceInBB(aa);
}
}
void parseJSONResponceInBB(字符串jsonInStrFormat)
{
试试{
JSONObject json=新的JSONObject(jsonInStrFormat);
JSONArray jArray=json.getJSONArray(“消息”);
//JSONArray arr=jArray.getJSONArray(0);
对于(inti=0;i你好,亲爱的,您需要使用黑莓的url扩展
所以请试着改变这一行
String aa=jsonresponse(url);
作为
成功完成从url下载数据后,请检查字符串aa是否获得任何值?如果它获得数据,请按照
试试这个,如果它工作正常,然后通过下面的链接
在中输入Url
String url="Your url";
String request=jsonresponse(url+";interface=wifi");
String response = parseJSONResponceInBB(request);
if(response .equalsIgnoreCase(""))
{
add(new LabelField("NO res"));
}
else
{
add(new LabelField(response ));
}
谢谢!您的代码帮助我解决了我的问题:)
String url="Your url";
String request=jsonresponse(url+";interface=wifi");
String response = parseJSONResponceInBB(request);
if(response .equalsIgnoreCase(""))
{
add(new LabelField("NO res"));
}
else
{
add(new LabelField(response ));
}