安卓 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());
    }
}

 

©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页