Android HttpURLConnection响应很奇怪

时间:2016-08-22 12:14:29

标签: java android http

我正在尝试获取Http请求的响应数据。我的代码如下所示:

public class Networking {

    // private variables
    private URL mUrl;
    private InputStream mInputStream;

    public void Networking() {}

    public InputStream setupConnection(String urlString) {

        // public variables
        int connectionTimeout = 10000; // milliseconds
        int readTimeout = 15000; // milliseconds

        try {
            mUrl = new URL(urlString);

            try {
                // initialize connection
                HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();

                // setup connection
                connection.setConnectTimeout(connectionTimeout);
                connection.setReadTimeout(readTimeout);
                connection.setRequestMethod("GET");
                connection.setDoInput(true);

                // start the query
                try {
                    connection.connect();
                    int response = connection.getResponseCode();

                    if (response == 200) {
                        // OK
                        mInputStream = connection.getInputStream();
                        return mInputStream;

                    } else if (response == 401) {
                        // Unauthorized
                        Log.e("Networking.setupConn...", "unauthorized HttpURL connection");
                    } else {
                        // no response code
                        Log.e("Networking.setupConn...", "could not discern response code");
                    }
                } catch (java.io.IOException e) {
                    Log.e("Networking.setupConn...", "error connecting");
                }


            } catch (java.io.IOException e) {
                Log.e("Networking.setupConn...", "unable to open HTTP Connection");
            }
        } catch (java.net.MalformedURLException e) {
            Log.e("Networking.setupConn..", "malformed url " + urlString);
        }

        // if could not get InputStream
        return null;
    }

    public String getStringFromInputStream() {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder(5000);
        String line;

        try {
            br = new BufferedReader(new InputStreamReader(mInputStream), 512);
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (java.io.IOException e) {
            Log.e("BufferReader(new ..)", e.toString());
            return null;
        } finally {
            if(br != null) {
                try {
                    br.close();
                }catch (java.io.IOException e) {
                    Log.e("br.close", e.toString());
                }
            }
        }
        return sb.toString();
    }
}

问题是getStringFromInputStream函数总是返回一个4063字节长的字符串。总是!无论什么网址。

我检查了,代码的(line = br.readLine())部分总是返回一个固定长度为4063的字符串。

我不明白这一点。请帮忙。

2 个答案:

答案 0 :(得分:-1)

我的代码对我有用:

public String getDataFromUrl(String httpUrlString)
            URL url = new URL(httpUrlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();
            responseCode = urlConnection.getResponseCode();
            if (responseCode != HttpStatus.SC_OK) {
                    return null;
            } else { // success
                BufferedReader in = null;
                StringBuffer str = new StringBuffer();
                try {
                    in = new BufferedReader(new InputStreamReader(
                            urlConnection.getInputStream()));
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        str.append(inputLine);
                    }
                } finally {
                    if (null != in) {
                        in.close();
                    }
                    urlConnection.disconnect();
                }
                return str.toString();
            }
}

答案 1 :(得分:-1)

在我看来,如果您使用库来获取http请求,它会对您有所帮助。 我可以建议retrofitvolley

除此之外,你可以尝试其他方法从InputStream中获取String,对here

有一个有趣的回复

我用过的那个是

BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
    buf.write((byte) result);
    result = bis.read();
}
return buf.toString();