I want to get data from server localhost using Blackberry simulator. My server give data into JSON file. How I can to display the data in Blackberry simulator and how to parse JSON into String. I wish some one help me.
This is my source code for server.
<?php
require('db.php');
$query="select*from penarikan";
$hasil=mysql_query($query);
if(mysql_num_rows($hasil)>0)
{
while($data=mysql_fetch_array($hasil))
{
$x[]=$data;
}
}
echo(json_encode($x));
?>
and this is my source code for Blackberry
package com.irwan.bb.pa;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONObject;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class ScreenViewPenarikan extends MainScreen {
HttpConnection httpconnection;
InputStream inputStream;
public ScreenViewPenarikan() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
runJson();
}
public void runJson()
{
try {
String url="http://127.0.0.1:80/proyek_akhir/view_penarikan.php;deviceside=true";
System.out.println(url);
//connect to server
httpconnection=(HttpConnection)Connector.open(url);
inputStream=httpconnection.openDataInputStream();
if(httpconnection.getResponseCode()==HttpConnection.HTTP_OK)
{
//add(new LabelField("Ada konesksi"));
InputStreamReader reader= new InputStreamReader(inputStream,"UTF-8");
int readCharacter;
StringBuffer responseBuffer = new StringBuffer();
while ((readCharacter = reader.read()) != -1) {
responseBuffer.append((char) readCharacter);
httpconnection.close();
inputStream.close();
reader.close();
String responseMessage = new String(responseBuffer);
JSONObject object = new JSONObject(responseMessage);
add(new LabelField(object));
}
}
else{
//add(new LabelField("Koneksi tidak ada"));
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
try this -
[
{
"0": "1",
"1": "2",
"2": "2013-06-30",
"3": "30110089",
"id_penarikan": "1",
"jlh_nominal": "2",
"tanggal": "2013-06-30",
"no_rekening": "30110089"
}
]
and
while ((readCharacter = reader.read()) != -1) {
responseBuffer.append((char) readCharacter);
}
String val="";
String responseMessage = new String(responseBuffer);
JSONArray j=new JSONArray(responseMessage);
JSONObject j0=j.getJSONObject(0);
val=j0.getString("id_penarikan");
add(new LabelField(val));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.