Android Http Get getStringFromConnection( HttpURLConnection connection)

Here you can find the source of getStringFromConnection( HttpURLConnection connection)

Description

get String From Connection

Declaration

public static String getStringFromConnection(
            HttpURLConnection connection) throws IOException 

Method Source Code

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

public class Main {
    public static String getStringFromConnection(
            HttpURLConnection connection) throws IOException {
        InputStream inputStream = new BufferedInputStream(
                connection.getInputStream());
        String jsonString = convertStreamToString(inputStream);
        inputStream.close();//from  w  w  w . j a  v  a2  s .c o m

        return jsonString;
    }

    private static String convertStreamToString(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream), 1024);
        StringBuilder stringBuilder = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }
}

Related

  1. executeHttpGet(String url)
  2. executeHttpPost(String url, ArrayList postParameters)
  3. getHTTP(String... params)
  4. handleURLEncodedResponse(HttpResponse response)
  5. handleXMLResponse(HttpResponse response)
  6. httpGetRequestParseParams( String paramString)
  7. formatHttpHeaders(Map headers)
  8. getStringResponseData(HttpResponse httpResponse)
  9. getHttpClient()