Blackberry シミュレーターを使用して、サーバー localhost からデータを取得したいと考えています。私のサーバーはJSONファイルにデータを渡します。Blackberry シミュレーターでデータを表示する方法と、JSON を文字列に解析する方法。誰かが私を助けてくれることを願っています。
これは、サーバーのソース コードです。
<?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));
?>
これが 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
}
}
}