get Json String from HttpResponse - Android Network

Android examples for Network:HTTP Response

Description

get Json String from HttpResponse

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String getJsonString(org.apache.http.HttpResponse response) {
        InputStream is = null;//from   w w  w.j  a v a  2 s  .c  om
        StringBuilder sb = null;

        try {
            is = response.getEntity().getContent();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is));

            sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
}

Related Tutorials