Read InputStream To String by charset - Android java.io

Android examples for java.io:InputStream

Description

Read InputStream To String by charset

Demo Code

import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class Main{

    public static String streamToString(InputStream in, String charset) {
        try {/* www  . j a  v a  2  s.co  m*/
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, charset));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return buffer.toString();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "";
    }

}

Related Tutorials