如何使用Blackberry Simulator从localhost服务器获取数据

时间:2013-06-24 06:01:03

标签: php json blackberry blackberry-simulator blackberry-eclipse-plugin

我想使用Blackberry模拟器从服务器localhost获取数据。我的服务器将数据提供给JSON文件。如何在Blackberry模拟器中显示数据以及如何将JSON解析为String。 我希望有人帮助我。

这是我的服务器源代码。

<?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
        }
    }

}

1 个答案:

答案 0 :(得分:1)

试试这个 -

[
    {
        "0": "1",
        "1": "2",
        "2": "2013-06-30",
        "3": "30110089",
        "id_penarikan": "1",
        "jlh_nominal": "2",
        "tanggal‌": "2013-06-30",
        "no_rekening": "30110089"
    }
] 

  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));