Java String to JSON conversion

后端 未结 4 936
遇见更好的自我
遇见更好的自我 2021-01-01 16:23

i am getting data from restful api in String variable now i want to convert to JSON object but i am having problem while conversion it throws exception .Here is my code :

4条回答
  •  独厮守ぢ
    2021-01-01 17:19

    You are getting NullPointerException as the "output" is null when the while loop ends. You can collect the output in some buffer and then use it, something like this-

        StringBuilder buffer = new StringBuilder();
        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            buffer.append(output);
        }
        output = buffer.toString(); // now you have the output
        conn.disconnect();
    

提交回复
热议问题