【发布时间】:2015-06-01 20:39:35
【问题描述】:
我正在关注 google 制作的 udacity 的 android 课程中的示例,我将示例的 url 替换为另一个示例的 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 字符串:{"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},"压力":995.73,"湿度":49,"天气": [{"id":500,"main":"Rain","description":"小雨","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},"压力":993.22,"湿度":46,"天气":[{"id":800,"main":"晴天","description":"天空晴朗","icon":" 01d"}],"speed":1.33,"deg":297,"clouds":0},{"dt":1427745600,"temp":{"day":16.45,"min":8.95,"max ":17.99,"夜晚":11.51,"前夕":17.99,"早上":8.95},"压力":1011.5,"湿度":0,"天气":[{"id":500,"main" :"Rain","description":"小雨","icon":"10d"}],"speed":2.24,"deg":292,"clouds":51},{"dt":1427832000, “温度”:{“天”:14.62,“分钟”:8.79,“最大”:18.33,“夜晚”:12.9,“前夕”:18.33 ,"早上":8.79},"压力":1014.65,"湿度":0,"天气":[{"id":500,"main":"雨","description":"小雨"," 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,"湿度":0,"weather":[{"id":800 ,"main":"Clear","description":"天空晴朗","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},"压力" :1011.2,"湿度":0,"天气":[{"id":800,"main":"晴天","description":"天空晴朗","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,"湿度":0,"weather":[{"id":800,"main":"Clear"," description":"天空晴朗","icon":"01d"}],"speed":3.81,"deg":338,"clouds":0}]}
【问题讨论】: