Note:
I have used "org.json.me" in this project.
CODE
public class jsonparsing extends UiApplication implements ListFieldCallback, FieldChangeListener
{
public static void main(String[] args)
{
jsonparsing app = new jsonparsing();
app.enterEventDispatcher();
}
MainScreen screen = new MainScreen();
public long mycolor ;
Font myFont1;
private static ListField _list;
private static Vector listElements = new Vector();
VerticalFieldManager hfm;
VerticalFieldManager subManager;
Connection _connectionthread;
RichTextField rt;
public jsonparsing()
{
super();
pushScreen(screen);
screen.setTitle("Blackberry Json Parser");
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbg.png");
hfm = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(),Display.getHeight(),backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR )
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
screen.add(hfm);
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
};
mycolor = 0x00FFFFFF;
_list.invalidate();
_list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER);
_list.setRowHeight(50);
_list.setCallback(this);
hfm.add(subManager);
rt = new RichTextField() {
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
_connectionthread = new Connection();
_connectionthread.start();
}
protected boolean navigationClick(int status, int time)
{
try
{
Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
if(focus == _list)
{
//UiApplication.getUiApplication().pushScreen(new screen3());
}
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
public boolean onSavePrompt()
{
return true;
}
private class Connection extends Thread
{
public Connection()
{
super();
}
public void run() {
try {
JSONArray result = new JSONArray(getJsonresponse());
int chk = result.length();
String opString[] = new String[chk];
for (int i = 0; i <17; i++)
{
opString[i] = result.getJSONObject(i).getString("text");
listElements.addElement(opString[i]);
}
}
catch (Exception e) {
System.out.println(e.toString());
rt.setText("catch1: "+e.toString());
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_list.setSize(listElements.size());
subManager.add(_list);
screen.invalidate();
}
});
}
}
public String getJsonresponse()
{
HttpConnection conn = null;
InputStream in = null;
String _response = null;
int code;
try {
conn = (HttpConnection) Connector.open("http://twitter.com/status/user_timeline/clusterlessons.json", Connector.READ);
conn.setRequestMethod(HttpConnection.GET);
code = conn.getResponseCode();
if (code == HttpConnection.HTTP_OK) {
in = conn.openInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[in.available()];
int len = 0;
while (-1 != (len = in.read(buffer))) {
out.write(buffer);
}
out.flush();
_response = new String(out.toByteArray());
System.out.println(_response);
//rt.setText(_response);
if (out != null){
out.close();
}
if (in != null){
in.close();
}
if (conn != null){
conn.close();
}
}
} catch (Exception e) {
System.out.println("HTTP Thread: " + e.toString());
Dialog.alert(e.toString());
}
return _response;
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String tes = (String)listElements.elementAt(index);
int yPos = 0+y;
g.drawText(tes, 10, 15+y, 0, w);
g.drawLine(0, yPos, w, yPos);
}
public Object get(ListField list, int index)
{
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String prefix, int string)
{
return listElements.indexOf(prefix, string);
}
public int getPreferredWidth(ListField list)
{
return Display.getWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void fieldChanged(Field field, int context) {
}
}
Your output will be like this.
That's it. JSON parsing done. You can download the sample project below.
i want to show only the first 10 litters , how ?
ReplyDelete