json-lib icon indicating copy to clipboard operation
json-lib copied to clipboard

IllegalStateException when parsing json string to json object

Open radonthetyrant opened this issue 9 years ago • 1 comments

Hello, here is the code snippet:

public boolean Login() {
        JSONObject o = new JSONObject()
                .put("apikey", apiKey)
                .put("username", userName)
                .put("userkey", userKey);
        RequestBody b = RequestBody.create(JSON, o.toString());
        Request r = new Request.Builder()
                .url(endPoint + "login")
                .post(b)
                .build();
        try {
            Response res = client.newCall(r).execute();
            System.out.println(res.body().string());
            JSONObject ro = (JSONObject) JSONSerializer.toJSON(res.body().string()); // Exception happens here
            token = ro.getString("token");
            //...............

prints out:

{
  "token": "eyJhbGciOiJSUzI1NiIsInR5cCI-----------snip----------PWp-6T5I36EYxuzC28IeTEH7XU3mtcdA"
}

Exception happening in: JSONObject ro = (JSONObject) JSONSerializer.toJSON(res.body().string());

java.lang.IllegalStateException: closed
	at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:377)
	at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:371)
	at okhttp3.internal.Util.bomAwareCharset(Util.java:412)
	at okhttp3.ResponseBody.string(ResponseBody.java:173)
	at MyApi.Login(MyApi.java:68)
	at App$1.run(App.java:175)

Is this a problem on my end? The token string is over 520 characters long. might the length be an issue?

radonthetyrant avatar Mar 06 '17 20:03 radonthetyrant

This appears to be an OkHTTP error given the stack trace. Json-lib ahs not had a chance to parse the data because the response body has not been fully read.

aalmiray avatar Sep 30 '22 21:09 aalmiray