1

i have to do a http GET request on the URL http://api.twitter.com/1/users/show.json?screen_name=Kaka and i will get a JSON object, but i don't know how i have to do it.

Anyone could help me?

Thanks you.

grouser
  • 618
  • 8
  • 23

1 Answers1

3

This BlackBerry code sample shows how you do that

Or, From another fairly simple example, that uses the org.json.me package added to BlackBerry Java 6.0:

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

Obviously, in the second example, you have to insert the names of the JSON keys that your JSON data actually uses (left as an exercise for the poster). Also, you'll probably know something about how the JSON objects are structured, as objects, and arrays, etc. So, your code for unpacking the JSON data into JSONObjects and JSONArrays may look a little different than above, depending on the structure of your own JSON data.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Upvoting. Very nice answer Nate. Could you take a look at this question here if you have some time? Is similar : http://stackoverflow.com/questions/16149958/http-post-request-on-blackberry/16151972?noredirect=1#16151972 – donparalias Apr 23 '13 at 07:33
  • Hey @Nate , any chance taking a look at the question? – donparalias Apr 23 '13 at 14:49
  • @donparalias, I agree with adwiv's answer to the question (also, see my comment below the answer). – Nate Apr 23 '13 at 22:47