Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: java.net.protocolException does not support output

I was previously using HttpClient and BasicNameValuePairs, for some reason i have to shift to HttpUrlConnection.

Hence this code, to make a HttpPost request with certain parameters:

public class MConnections {

    static String BaseURL = "http://www.xxxxxxxxx.com";
    static String charset = "UTF-8";
    private static String result;
    private static StringBuilder sb;
    private static List<String> cookies = new ArrayList<String>();

    public static String PostData(String url, String sa[][]) {

        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) new URL(BaseURL + url)
                    .openConnection();
        } catch (MalformedURLException e1) {
        } catch (IOException e1) {
        }

        cookies = connection.getHeaderFields().get("Set-Cookie");
        try{
        connection.setDoOutput(true); // Triggers POST.
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=" + charset);
        }catch (Exception e) {

            //Here i get Exception that "java.lang.IllegalStateException: Already connected"
        }
        OutputStream output = null;
        String query = "";
        int n = sa.length;
        for (int i = 0; i < n; i++) {
            try {
                query = query + sa[i][0] + "="
                        + URLEncoder.encode(sa[i][1], "UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
        }
        try {
            output = connection.getOutputStream();
            output.write(query.getBytes(charset));
        } catch (Exception e) {

            //Here i get Exception that "android: java.net.protocolException: Does not support output"
        } finally {

            if (output != null)
                try {
                    output.close();
                } catch (IOException e) {
                }
        }
        InputStream response = null;
        try {
            response = connection.getInputStream();
        } catch (IOException e) {

            //Here i get Exception that "java.io.IOException: BufferedInputStream is closed"
        } finally {

            //But i am closing it here
            connection.disconnect();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine());
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append("\n" + line);
            }
            response.close();
            result = sb.toString();
        } catch (Exception e) {
        }
        return result;
    }
}

But i get such Exceptions as commented in the code.

Actually i am calling MConnections.PostData() twice from my Activity using a AsyncTask. This might cause the Exception: Already Connected but i am using connection.disconnect. But why am i still getting that Exception?

Am i using it the wrong way?

Thank You

like image 304
Archie.bpgc Avatar asked Nov 06 '12 09:11

Archie.bpgc


1 Answers

For the protocol exception, try adding the following before you call getOutputStream():

connection.setDoOutput(true);

Discovered this answer thanks to Brian Roach's answer here: https://stackoverflow.com/a/14026377/387781

Side note: I was having this issue on my HTC Thunderbolt running Gingerbread, but not on my Nexus 4 running Jelly Bean.

like image 107
SilithCrowe Avatar answered Oct 26 '22 23:10

SilithCrowe