我想使用黑莓模擬器從服務器本地主機獲取數據。我的服務器將數據提供給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));
?>
,這是黑莓
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
}
}
}
您正從服務器的響應? – Signare
是的,我得到服務器的響應。 –
該回復的格式是什麼? – Signare