Android HttpResponse Read decodeJSONResponse(HttpResponse resp)

Here you can find the source of decodeJSONResponse(HttpResponse resp)

Description

decode JSON Response

Declaration

protected static JSONObject decodeJSONResponse(HttpResponse resp) 

Method Source Code

//package com.java2s;

import android.util.Log;

import org.apache.http.HttpResponse;

import org.json.JSONObject;
import org.json.JSONException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    protected static String TAG = "JSONServerNetworkUtil";

    protected static JSONObject decodeJSONResponse(HttpResponse resp) {

        InputStream is = null;//  w  ww  . j  av a  2 s. co  m
        try {
            is = resp.getEntity().getContent();
        } catch (IOException e) {
            Log.d(TAG, "unable to get content from response entity");
            e.printStackTrace();
            return null;
        }

        String in = convertStreamToString(is);

        JSONObject json = null;
        try {
            json = new JSONObject(in);
        } catch (JSONException e) {
            Log.d(TAG, "could not decode JSON response from: " + in);
        }

        return json;
    }

    protected static String convertStreamToString(InputStream is) {

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

        String line = null;
        try {
            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

  1. readBody(HttpResponse resp, ByteBuffer target)
  2. handleJsonResponse(HttpResponse response)