安卓 Android Java HttpURLConnection getContentLength() = -1,inputStream.available() = 0 解决方案

这不知道是安卓的读取机制还是安卓的BUG,

正常的java都能拿到返回值,唯独安卓不行。

解决方案:

    static String getResponseMessage(HttpURLConnection connection) throws Exception {
        InputStream inputStream = connection.getInputStream();
        ByteArrayOutputStream bufferReader = new ByteArrayOutputStream();
        int b;
        while ((b = inputStream.read()) != -1)
            bufferReader.write(b);
        inputStream.close();
        String message = bufferReader.toString();
        bufferReader.close();
        return message;
    }

HttpUtil:

package xxx;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpUtil {

    enum Method {
        get,
        post
    }

    static String getResponseMessage(HttpURLConnection connection) throws Exception {
        InputStream inputStream = connection.getInputStream();
        ByteArrayOutputStream bufferReader = new ByteArrayOutputStream();
        int b;
        while ((b = inputStream.read()) != -1)
            bufferReader.write(b);
        inputStream.close();
        String message = bufferReader.toString();
        bufferReader.close();
        return message;
    }

    static HttpURLConnection send(String uri, Method method, String data, Map<String, String> headers) throws Exception {
        URL url = new URL(uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        switch (method) {
            case post:
                connection.setRequestMethod("POST");
                break;
            default:
                connection.setRequestMethod("GET");
                break;
        }
        System.out.println("Http " + method + " " + uri);

        if (headers != null)
            for (Map.Entry<String, String> header : headers.entrySet())
                connection.setRequestProperty(header.getKey(), header.getValue());

        if (data != null) {
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.close();
        }

        connection.connect();
        System.out.println("Http response code " + connection.getResponseCode());
        return connection;
    }

    public static HttpURLConnection get(String uri, Map<String, String> headers) throws Exception {
        return send(uri, Method.get, null, headers);
    }

    public static HttpURLConnection get(String url) throws Exception {
        return get(url, null);
    }

    public static String getString(String uri, Map<String, String> headers) throws Exception {
        HttpURLConnection connection = get(uri, headers);
        //String result = connection.getResponseMessage();//安卓不可用
        String result = getResponseMessage(connection);
        connection.disconnect();
        return result;
    }

    public static String getString(String url) throws Exception {
        return getString(url, null);
    }

    public static HttpURLConnection post(String uri, String data, Map<String, String> headers) throws Exception {
        return send(uri, Method.post, data, headers);
    }

    public static HttpURLConnection post(String url, String data) throws Exception {
        return post(url, data, null);
    }

    public static String postJson(String url, String json) throws Exception {
        Map<String, String> headers = new HashMap();
        headers.put("Content-Type", "application/json");
        HttpURLConnection connection = post(url, json, headers);
        //String result = connection.getResponseMessage();//安卓不可用
        String result = getResponseMessage(connection);
        connection.disconnect();
        return result;
    }

    public static String post(String url, JSONObject data) throws Exception {
        return postJson(url, data.toString());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海好程序员

给上海好程序员加个鸡腿!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值