0

我想使用黑莓模拟器从服务器本地主机获取数据。我的服务器将数据输入 JSON 文件。如何在黑莓模拟器中显示数据以及如何将 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
        }
    }

}
4

1 回答 1

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));
于 2013-06-24T07:21:36.133 回答