使用HttpUrlConnection可以获得多少数据

时间:2015-03-28 15:33:48

标签: android httpurlconnection

我正在关注google制作的udacity的android课程中的示例,我用另一个替换了该示例的url,但我只能看到,使用LOG只是我正在下载的url的一小部分。

public class FetchWeatherTask extends AsyncTask<Void, Void, Void> {
    private  final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
    @Override
    protected Void doInBackground(Void... params) {
        // These two need to be declared outside the try/catch
        // so that they can be closed in the finally block.
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        // Will contain the raw JSON response as a string.
        String forecastJsonStr = null;

        try {
            // Construct the URL for the OpenWeatherMap query
            // Possible parameters are available at OWM's forecast API page, at
            // http://openweathermap.org/API#forecast
            URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7");

            // Create the request to OpenWeatherMap, and open the connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                // Nothing to do.
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                // But it does make debugging a *lot* easier if you print out the completed
                // buffer for debugging.
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty.  No point in parsing.
                return null;
            }
            forecastJsonStr = buffer.toString();
            Log.v(LOG_TAG, "Forecast JSON String: " + forecastJsonStr);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error ", e);
            // If the code didn't successfully get the weather data, there's no point in attemping
            // to parse it.
            return null;
        } finally{
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e(LOG_TAG, "Error closing stream", e);
                }
            }
        }

        return null;
    }
}

您可以下载多少数据?

我在日志中的回复:

  

V / FetchWeatherTask:预测JSON字符串:{&#34; cod&#34;:&#34; 200&#34;,&#34; message&#34;:0.0506,&#34; city&#34;: {&#34; id&#34;:0,&#34; name&#34;:&#34; Mountain View&#34;,&#34; country&#34;:&#34; US&#34;,& #34; COORD&#34; {&#34; LAT&#34;:37.4056&#34; LON&#34;: - 122.0775}},&#34; CNT&#34;:7,&#34;列表& #34;:[{&#34峰; dt&#34;:1427572800,&#34;温度&#34; {&#34;天&#34;:22.34&#34;分钟&#34;:6.06,& #34; MAX&#34;:22.41&#34;夜间&#34;:6.06,&#34;前夕&#34;:16.73&#34;早晨&#34;:9.62}&#34;压力&# 34;:995.73&#34;湿度&#34;:49,&#34;天气&#34;:[{&#34; ID&#34; 500&#34;主&#34;:&#34 ; Rain&#34;,&#34;描述&#34;:&#34;小雨&#34;,&#34; icon&#34;:&#34; 10d&#34;}],&#34;速度& #34;:1.95,&#34;度&#34;:339,&#34;云&#34; 32},{&#34峰; dt&#34;:1427659200,&#34;温度&#34 ;: {&#34;天&#34;:23.84&#34;分钟&#34;:5.16,&#34; MAX&#34;:23.84&#34;夜间&#34;:6.98,&#34;前夕& #34;:16.84&#34;早晨&#34;:5.16}&#34;压力&#34;:993.22&#34;湿度&#34;:46,&#34;天气&#34;:[ {&#34; ID&#34; 800&#34;主&#34;:&#34;清除&#34;&#34;记述&#34;:&#34;天空晴朗&#34;,&#34;图标&#34;:&#34; 01d&#34;}],&#34;速度&#34;:1.33,&#34 ;度&#34;:297,&#34;云&#34;:0},{&#34峰; dt&#34;:1427745600,&#34;温度&#34; {&#34;天&#34; :16.45&#34;分钟&#34;:8.95,&#34; MAX&#34;:17.99&#34;夜间&#34;:11.51&#34;前夕&#34;:17.99&#34 ;早晨&#34;:8.95}&#34;压力&#34;:1011.5,&#34;湿度&#34;:0,&#34;天气&#34;:[{&#34; ID&#34; :500,&#34;主要&#34;:&#34; Rain&#34;,&#34;描述&#34;:&#34;小雨&#34;,&#34; icon&#34;:& #34; 10D&#34;}],&#34;速度&#34;:2.24,&#34;度&#34;:292,&#34;云&#34; 51},{&#34; DT& #34;:1427832000,&#34;温度&#34; {&#34;天&#34;:14.62&#34;分钟&#34;:8.79,&#34; MAX&#34;:18.33,& #34;夜&#34;:12.9,&#34;前夕&#34;:18.33&#34;早晨&#34;:8.79}&#34;压力&#34;:1014.65&#34;湿度&# 34;:0,&#34;天气&#34;:[{&#34; ID&#34; 500&#34;主&#34;:&#34;雨&#34;&#34;描述& #34;:&#34;小雨&#34;,&#34; icon&#34;:&#34; 10d&#34;}],&#34;速度&#34;:4.02,&#34; deg& #34;:324,&#34;云&#34;:0},{&#34峰; dt&#34;:1427918400,&#34;温度&#34; {&#34;天&#34;:1 5.37&#34;分钟&#34;:9.92,&#34; MAX&#34;:17.5,&#34;夜间&#34;:14.15&#34;前夕&#34;:17.5,&#34;早晨&#34;:9.92}&#34;压力&#34;:1012.74&#34;湿度&#34;:0,&#34;天气&#34;:[{&#34; ID&#34 ;: 800,&#34; main&#34;:&#34;清除&#34;,&#34;描述&#34;:&#34;天空晴朗&#34;,&#34; icon&#34;:& #34; 01D&#34;}],&#34;速度&#34;:3.46,&#34;度&#34;:312,&#34;云&#34;:5},{&#34; DT& #34;:1428004800,&#34;温度&#34; {&#34;天&#34;:14.2,&#34;分钟&#34;:10.92&#34; MAX&#34;:16.56,& #34;夜&#34;:14.1,&#34;前夕&#34;:16.56&#34;早晨&#34;:10.92}&#34;压力&#34;:1011.2,&#34;湿度&# 34;:0,&#34;天气&#34;:[{&#34; ID&#34; 800&#34;主&#34;:&#34;清除&#34;&#34;描述& #34;:&#34;天空晴朗&#34;,&#34;图标&#34;:&#34; 01d&#34;}],&#34;速度&#34;:6.67,&#34;度&#34;:330,&#34;云&#34;:0},{&#34峰; dt&#34;:1428091200,&#34;温度&#34; {&#34;天&#34 ;: 16.39&#34;分钟&#34;:10.38&#34; MAX&#34;:17.89&#34;夜间&#34;:11.01&#34;前夕&#34;:17.89&#34;早晨&#34;:10.38}&#34;压力&#34;:1014.68&#34;湿度&#34;:0,&#34;天气&#34 ;: [{&#34; id&#34;:800,&#34; main&#34;:&#34; Clear&#34;,&#34; description&#34;:&#34; sky is clear&#34; &#34;图标&#34;:&#34; 01D&#34;}],&#34;速度&#34;:3.81,&#34;度&#34;:338,&#34;云&#34 ;:0}]}

1 个答案:

答案 0 :(得分:1)

肯定存在一个限制,对于较大的日志,例如json响应,总是尝试对响应进行分段(json响应是对象的集合,如项目数组,尝试将响应转换为jason数组并逐个获取)并打印几个日志函数,而不是尝试在单个日志函数中打印整个响应。