2014年10月14日 19:06 C:\temp\WebAPILoader.java package jp.android.booksample.webapisample; import import import import import import import import import import import import java.io.BufferedReader; java.io.File; java.io.FileInputStream; java.io.FileOutputStream; java.io.IOException; java.io.InputStream; java.io.InputStreamReader; java.net.HttpURLConnection; java.net.MalformedURLException; java.net.URL; java.util.Calendar; java.util.Date; import org.json.JSONException; import org.json.JSONObject; import android.content.AsyncTaskLoader; import android.content.Context; public class WebAPILoader extends AsyncTaskLoader<JSONObject> { /** * ここ読み込み処処ででででででで有有にににににににで示に値でに。 */ private boolean mEnableCache; /** * 天天で取取にに都都IDでに。 */ private int mCityId; /** * APIこURLでに。 */ private final static String API_URL = "http://weather.livedoor.com/forecast/webservice/json/v1?city=%1$d" ; /** * 天天天天このののの名で示ししに。 */ private static final String CACHEFILE = "weather.json"; /** * でででででキ無有ととにしでこ時時時で示に値でに(3時時)。 */ private static final int CACHE_TIMES = 3; public WebAPILoader(Context context, boolean enableCache, int cityId) { super(context); this.mEnableCache = enableCache; this.mCityId = cityId; } /** * ロロロロ処処処処時処び出ささにささでさでに。 ロロさ処処処処で行いしに。 -1- 2014年10月14日 19:06 C:\temp\WebAPILoader.java */ @Override protected void onStartLoading() { forceLoad(); super.onStartLoading(); } /** * ササササでさで行いいい処処で記記ししに。 */ @Override public JSONObject loadInBackground() { URL url; JSONObject result = null; try { File cachefile = new File(getContext().getCacheDir(), CACHEFILE); Calendar calendar = Calendar.getInstance(); if (cachefile.exists()) { calendar.setTime(new Date(cachefile.lastModified())); calendar.add(Calendar.HOUR, CACHE_TIMES); } String json; if (!mEnableCache || !cachefile.exists() || calendar.getTime().before(new Date())) { // でででででののののキ無有と場場は、再読込で行に // HTTPコココでココで作作にに url = new URL(String.format(API_URL, mCityId)); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); // 通通で処処にに connection.connect(); // JSONデロデで取取にに json = loadInputStream(connection.getInputStream()); // 読み込んんデロデででででででデロデとしん保保にに FileOutputStream cache = null; try { cache = new FileOutputStream(cachefile); cache.write(json.getBytes()); } finally { if (cache != null) { cache.close(); } } } else { // でででででののののキ有有と場場、でででででのののので読み込む InputStream stream = new FileInputStream(cachefile); json = loadInputStream(stream); } // 取取しいデロデでJSONオサオオコオに変変にに result = new JSONObject(json); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { -2- 2014年10月14日 19:06 C:\temp\WebAPILoader.java e.printStackTrace(); } return result; } /** * サオスロスにス文文文で取取ししに。 * * @param stream 入入サオスロス * * @return サオスロスにス読み込しさい文文文 * @throws IOException 入出入エエロ * */ private String loadInputStream(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); // ササレコサ文文文で取取にに String line; StringBuilder lines = new StringBuilder(); while ((line = reader.readLine()) != null) { lines.append(line); } return lines.toString(); } } -3-
© Copyright 2025 ExpyDoc