4

The response json:

{
    "filename": "vcops-6.0.0-MPforAWS-1.1-1695068.pak",
    "links": [
        {
            "rel": "pak_information",
            "href": "https://<IP>:443/casa/upgrade/cluster/pak/MPforAWS-600/information"
        },
        {
            "rel": "pak_file_information",
            "href": "https://<IP>:443/casa/upgrade/slice/pak/MPforAWS-600/file_information"
        },
        {
            "rel": "pak_cluster_status",
            "href": "https://<IP>:443/casa/upgrade/cluster/pak/MPforAWS-600/status"
        }
    ],
    "pak_id": "MPforAWS-600"
}

I am using one helper of the framework we have. Framework returns response as "InputStream". I want to get "pak_id" from this "InputStream". I tried with inputStreamObj.toString() this does not work for me.

The method I am using is:

private String getPakId(InputStream uploadResponse) {
    String pakId = null;
    try {
        String responseString = readInputStream(uploadResponse);
        JSONObject jObj = new JSONObject(responseString);
        pakId = jObj.getString("pak_id").trim();
        Reporter.log("Pak id is=" + pakId, true);
    } catch (Exception e) {
        Reporter.log("Error in getting pak_id " + e.getMessage(), true);
    }
    return pakId;
}

and

private String readInputStream(InputStream inputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            inputStream, "UTF-8"));
    String tmp;
    StringBuilder sb = new StringBuilder();
    while ((tmp = reader.readLine()) != null) {
        sb.append(tmp).append("\n");
    }
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
        sb.setLength(sb.length() - 1);
    }
    reader.close();
    return sb.toString();
}
5
  • And what is your current code? Commented Apr 14, 2014 at 9:56
  • You have to read the String from your InputStream and then parse it using JSON library. Commented Apr 14, 2014 at 9:56
  • possible duplicate of stackoverflow.com/questions/6511880/… Commented Apr 14, 2014 at 9:58
  • JSONObject jObj = new JSONObject(response.toString()); pakId = jObj.getString("pak_id").trim(); Commented Apr 14, 2014 at 10:20
  • The question is duplicate. Try this solution: stackoverflow.com/a/47265988/1264496 Commented Nov 15, 2017 at 11:13

3 Answers 3

2

If you look at the documentation for InputStream, you'll notice it will not promise you that toString will present you the contents of the stream.

If you're not interested in actually streaming the stream (which is reasonable if you expect the response to be small, like it appears to be the case here), it's OK to first get all the bytes from the stream, put them into a String, and then parse the String.

To get the String out of the InputStream, I'd recommend IOUtils.toString from apache commons-io.

Sign up to request clarification or add additional context in comments.

Comments

0

If you only want the pak_id value using only the standard library:

InputStream is = ...
String line;
StringBuilder text = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while((line = reader.readLine()) != null) {
    text.append(line).append(" ");
}
String pakId = text.toString().replaceAll(".*\"pak_id\": \"([^\"]+)\".*", "$1");

2 Comments

What I want to say, I have this response stored in "InputStream" object, and I need to retrieve "pak_id"
I updated my answer so now you can obtain the pak_id value directly from the InputStream
-1

Well, better late than never... the following worked for me:

JSONTokener tokener = new JSONTokener(new InputStreamReader(istream));
JSONObject result;
try {
    result = new JSONObject(tokener);
} catch (JSONException e) {
    // Handle me!
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.