1

URLhttp://api.twitter.com/1/users/show.json?screen_name=KakaでhttpGETリクエストを実行する必要があり、JSONオブジェクトを取得しますが、どのように実行する必要があるのか​​わかりません。やれ。

誰かが私を助けることができますか?

ありがとうございます。

4

1 に答える 1

3

このBlackBerryコードサンプルは、その方法を示しています

または、BlackBerry Java 6.0に追加されたパッケージを使用する別の非常に単純な例からorg.json.me

  HttpConnection conn = null;
  InputStream in = null;
  ByteArrayOutputStream out = null;
  try {
     String url = "http://api.twitter.com/1/users/show.json?screen_name=Kaka";
     conn = (HttpConnection) Connector.open(url, Connector.READ);
     conn.setRequestMethod(HttpConnection.GET);

     int code = conn.getResponseCode();
     if (code == HttpConnection.HTTP_OK) {
        in = conn.openInputStream();
        out = new ByteArrayOutputStream();
        byte[] buffer = new byte[in.available()];
        int len = 0;
        while ((len = in.read(buffer)) > 0) {
           out.write(buffer);
        }
        out.flush();
        String response = new String(out.toByteArray());
        JSONObject resObject = new JSONObject(response);
        String key = resObject.getString("Insert Json Key");

        Vector resultsVector = new Vector();
        JSONArray jsonArray = resObject.getJSONArray("Insert Json Array Key");
        if (jsonArray.length() > 0) {
           for (int i = 0; i < jsonArray.length();i++) {
              Vector elementsVector = new Vector();
              JSONObject jsonObj = jsonArray.getJSONObject(i);
              elementsVector.addElement(jsonObj.getString("Insert Json Array Element Key1"));
              elementsVector.addElement(jsonObj.getString("Insert Json Array Element Key2"));
              resultsVector.addElement(elementsVector);
           }
        }
      }
  } catch (Exception e) {
     Dialog.alert(e.getMessage());
  } finally {
     if (out != null) {
        out.close();
     }
     if (in != null) {
        in.close();
     }
     if (conn != null) {
        conn.close();
     }
  }

明らかに、2番目の例では、JSONデータが実際に使用するJSONキーの名前を挿入する必要があります(ポスターの演習として残されています)。また、JSONオブジェクトがオブジェクトや配列などとしてどのように構造化されているかについても知っていると思います。したがって、JSONデータをJSONObjectsとJSONArraysに解凍するコードは、の構造によっては上記とは少し異なる場合があります。独自のJSONデータ。

于 2012-05-18T22:29:27.173 に答える