get HTTP Response As String - Android Network

Android examples for Network:HTTP Response

Description

get HTTP Response As String

Demo Code


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

import java.io.StringWriter;

import java.net.HttpURLConnection;

import java.util.zip.GZIPInputStream;

import android.text.TextUtils;

public class Main {
    public static final String DEFAULT_CHARSET = "UTF-8";

    protected static String getResponseAsString(HttpURLConnection conn,
            boolean responseError) throws IOException {
        String charset = getResponseCharset(conn.getContentType());
        String header = conn.getHeaderField("Content-Encoding");
        boolean isGzip = false;
        if (header != null && header.toLowerCase().contains("gzip")) {
            isGzip = true;/*from  w w w.ja  va  2s .  c o m*/
        }
        InputStream es = conn.getErrorStream();
        if (es == null) {
            InputStream input = conn.getInputStream();
            if (isGzip) {
                input = new GZIPInputStream(input);
            }
            return getStreamAsString(input, charset);
        } else {
            if (isGzip) {
                es = new GZIPInputStream(es);
            }
            String msg = getStreamAsString(es, charset);
            if (TextUtils.isEmpty(msg)) {
                throw new IOException(conn.getResponseCode() + ":"
                        + conn.getResponseMessage());
            } else if (responseError) {
                return msg;
            } else {
                throw new IOException(msg);
            }
        }
    }

    private static String getResponseCharset(String ctype) {
        String charset = DEFAULT_CHARSET;

        if (!TextUtils.isEmpty(ctype)) {
            String[] params = ctype.split(";");
            for (String param : params) {
                param = param.trim();
                if (param.startsWith("charset")) {
                    String[] pair = param.split("=", 2);
                    if (pair.length == 2) {
                        if (!TextUtils.isEmpty(pair[1])) {
                            charset = pair[1].trim();
                        }
                    }
                    break;
                }
            }
        }

        return charset;
    }

    private static String getStreamAsString(InputStream stream,
            String charset) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(stream, charset));
            StringWriter writer = new StringWriter();

            char[] chars = new char[256];
            int count = 0;
            while ((count = reader.read(chars)) > 0) {
                writer.write(chars, 0, count);
            }

            return writer.toString();
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }
}

Related Tutorials