使用 HttpUrlConnection 可以获得多少数据
How much data can you get using 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﹕ Forecast JSON String: {"cod":"200","message":0.0506,"city":{"id":0,"name":"Mountain View","country":"US","coord":{"lat":37.4056,"lon":-122.0775}},"cnt":7,"list":[{"dt":1427572800,"temp":{"day":22.34,"min":6.06,"max":22.41,"night":6.06,"eve":16.73,"morn":9.62},"pressure":995.73,"humidity":49,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":1.95,"deg":339,"clouds":32},{"dt":1427659200,"temp":{"day":23.84,"min":5.16,"max":23.84,"night":6.98,"eve":16.84,"morn":5.16},"pressure":993.22,"humidity":46,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":1.33,"deg":297,"clouds":0},{"dt":1427745600,"temp":{"day":16.45,"min":8.95,"max":17.99,"night":11.51,"eve":17.99,"morn":8.95},"pressure":1011.5,"humidity":0,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":2.24,"deg":292,"clouds":51},{"dt":1427832000,"temp":{"day":14.62,"min":8.79,"max":18.33,"night":12.9,"eve":18.33,"morn":8.79},"pressure":1014.65,"humidity":0,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":4.02,"deg":324,"clouds":0},{"dt":1427918400,"temp":{"day":15.37,"min":9.92,"max":17.5,"night":14.15,"eve":17.5,"morn":9.92},"pressure":1012.74,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.46,"deg":312,"clouds":5},{"dt":1428004800,"temp":{"day":14.2,"min":10.92,"max":16.56,"night":14.1,"eve":16.56,"morn":10.92},"pressure":1011.2,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":6.67,"deg":330,"clouds":0},{"dt":1428091200,"temp":{"day":16.39,"min":10.38,"max":17.89,"night":11.01,"eve":17.89,"morn":10.38},"pressure":1014.68,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.81,"deg":338,"clouds":0}]}
肯定是有限制的,对于较大的日志,例如json响应,总是尝试对响应进行分段(json响应是对象的集合,如项目数组,尝试将响应转换为jason 数组并一个一个地获取)并打印多个日志函数,而不是尝试在单个日志函数中打印整个响应。
我正在学习 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﹕ Forecast JSON String: {"cod":"200","message":0.0506,"city":{"id":0,"name":"Mountain View","country":"US","coord":{"lat":37.4056,"lon":-122.0775}},"cnt":7,"list":[{"dt":1427572800,"temp":{"day":22.34,"min":6.06,"max":22.41,"night":6.06,"eve":16.73,"morn":9.62},"pressure":995.73,"humidity":49,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":1.95,"deg":339,"clouds":32},{"dt":1427659200,"temp":{"day":23.84,"min":5.16,"max":23.84,"night":6.98,"eve":16.84,"morn":5.16},"pressure":993.22,"humidity":46,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":1.33,"deg":297,"clouds":0},{"dt":1427745600,"temp":{"day":16.45,"min":8.95,"max":17.99,"night":11.51,"eve":17.99,"morn":8.95},"pressure":1011.5,"humidity":0,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":2.24,"deg":292,"clouds":51},{"dt":1427832000,"temp":{"day":14.62,"min":8.79,"max":18.33,"night":12.9,"eve":18.33,"morn":8.79},"pressure":1014.65,"humidity":0,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":4.02,"deg":324,"clouds":0},{"dt":1427918400,"temp":{"day":15.37,"min":9.92,"max":17.5,"night":14.15,"eve":17.5,"morn":9.92},"pressure":1012.74,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.46,"deg":312,"clouds":5},{"dt":1428004800,"temp":{"day":14.2,"min":10.92,"max":16.56,"night":14.1,"eve":16.56,"morn":10.92},"pressure":1011.2,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":6.67,"deg":330,"clouds":0},{"dt":1428091200,"temp":{"day":16.39,"min":10.38,"max":17.89,"night":11.01,"eve":17.89,"morn":10.38},"pressure":1014.68,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.81,"deg":338,"clouds":0}]}
肯定是有限制的,对于较大的日志,例如json响应,总是尝试对响应进行分段(json响应是对象的集合,如项目数组,尝试将响应转换为jason 数组并一个一个地获取)并打印多个日志函数,而不是尝试在单个日志函数中打印整个响应。