• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Révision97f614a850a22f8a4f8848ef727cb38a40da0a70 (tree)
l'heure2018-08-02 02:22:42
AuteurHMML <hmml3939@gmai...>
CommiterHMML

Message de Log

Try to replace ion with HttpURLConnection for workaround of weird semaphore lock error...

Change Summary

Modification

--- a/app/build.gradle
+++ b/app/build.gradle
@@ -7,8 +7,8 @@ android {
77 applicationId "cloud.hmml.mmw"
88 minSdkVersion 17
99 targetSdkVersion 27
10- versionCode 7
11- versionName '0.4.3'
10+ versionCode 8
11+ versionName '0.4.4'
1212 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
1414 }
--- a/app/src/main/java/cloud/hmml/mmw/ConfigManager.java
+++ b/app/src/main/java/cloud/hmml/mmw/ConfigManager.java
@@ -645,9 +645,7 @@ public class ConfigManager implements GoogleApiClient.ConnectionCallbacks, Googl
645645 String ns = loadLocationNS();
646646
647647 if (ns != null && ns.equals(LOCATION_NS_JMA)) {
648- if (!(new ForecastFetcher(this.getContext(), ns, loadLocationId())).save(this)) {
649- Log.e("cmgr", "Failed to get JMA forecat!");
650- }
648+ new ForecastFetcher(this.getContext(), ns, loadLocationId()).save(this);
651649 }
652650 }
653651
--- a/app/src/main/java/cloud/hmml/mmw/ForecastFetcher.java
+++ b/app/src/main/java/cloud/hmml/mmw/ForecastFetcher.java
@@ -1,17 +1,24 @@
11 package cloud.hmml.mmw;
22
33 import android.content.Context;
4+import android.os.AsyncTask;
5+import android.os.Build;
46 import android.util.Log;
57
68 import com.google.gson.JsonElement;
79 import com.google.gson.JsonObject;
8-import com.koushikdutta.ion.Ion;
10+import com.google.gson.JsonParser;
11+import com.google.gson.stream.JsonReader;
912
13+import java.io.IOException;
14+import java.io.InputStreamReader;
15+import java.net.HttpURLConnection;
16+import java.net.MalformedURLException;
17+import java.net.URL;
1018 import java.text.ParseException;
1119 import java.text.SimpleDateFormat;
1220 import java.util.Calendar;
1321 import java.util.Date;
14-import java.util.concurrent.ExecutionException;
1522
1623
1724 public class ForecastFetcher {
@@ -26,76 +33,137 @@ public class ForecastFetcher {
2633 this.context = context;
2734 }
2835
29- public boolean save(ConfigManager config) {
30- JsonObject json = getJson();
31- if (json == null) {
32- Log.e("forecast-fetcher", "Failed to fetch forecast JSON.");
33- return false;
34- }
36+ public void save(ConfigManager config) {
37+ this.save(config, null);
38+ }
3539
36- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
37- Date reportedAt = null;
38- Calendar reportedAtCal = null;
39- try {
40- reportedAt = df.parse(json.getAsJsonPrimitive("reported_at").getAsString());
41- config.saveReportedAt(reportedAt);
42- } catch (ParseException e) {
43- Log.e("forecast-fetcher", "Failed to parse reported_at!");
44- e.printStackTrace();
45- } catch (NullPointerException e) {
46- // ignore!
47- }
4840
49- int index = 1;
50- for (JsonElement j: json.getAsJsonArray("daily_forecasts")) {
51- JsonObject day = j.getAsJsonObject();
41+ public void save(ConfigManager config, Runnable callback) {
5242
53- String label = day.getAsJsonPrimitive("weather_label").getAsString();
54- String ident = day.getAsJsonPrimitive("weather_symbol").getAsString();
55- Integer temp_max = null;
56- Integer temp_min = null;
57- Integer pop = null;
58- try {
59- temp_max = day.getAsJsonPrimitive("temp_max").getAsInt();
60- } catch (ClassCastException e) {
61- // ignroe
62- }
63- try {
64- temp_min = day.getAsJsonPrimitive("temp_min").getAsInt();
65- } catch (ClassCastException e) {
66- // ignroe
43+ class JsonFetcher extends AsyncTask<Void, Void, JsonObject> {
44+ private ConfigManager config;
45+ private Runnable callback;
46+
47+ public JsonFetcher(ConfigManager config, Runnable callback) {
48+ this.config = config;
49+ this.callback = callback;
6750 }
68- try {
69- pop = day.getAsJsonPrimitive("pop").getAsInt();
70- } catch (ClassCastException e) {
71- // ignroe
51+
52+ @Override
53+ protected JsonObject doInBackground(Void... voids) {
54+ return ForecastFetcher.this.getJson();
7255 }
7356
74- config.saveWeather(index, ident, label, temp_min, temp_max, pop);
57+ @Override
58+ protected void onPostExecute(JsonObject json) {
59+ if (json == null) {
60+ Log.e("forecast-fetcher", "Failed to fetch forecast JSON.");
61+ return;
62+ }
63+
64+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
65+ Date reportedAt = null;
66+ Calendar reportedAtCal = null;
67+ try {
68+ reportedAt = df.parse(json.getAsJsonPrimitive("reported_at").getAsString());
69+ config.saveReportedAt(reportedAt);
70+ } catch (ParseException e) {
71+ Log.e("forecast-fetcher", "Failed to parse reported_at!");
72+ e.printStackTrace();
73+ } catch (NullPointerException e) {
74+ // ignore!
75+ }
76+
77+ int index = 1;
78+ for (JsonElement j: json.getAsJsonArray("daily_forecasts")) {
79+ JsonObject day = j.getAsJsonObject();
80+
81+ String label = day.getAsJsonPrimitive("weather_label").getAsString();
82+ String ident = day.getAsJsonPrimitive("weather_symbol").getAsString();
83+ Integer temp_max = null;
84+ Integer temp_min = null;
85+ Integer pop = null;
86+ try {
87+ temp_max = day.getAsJsonPrimitive("temp_max").getAsInt();
88+ } catch (ClassCastException e) {
89+ // ignroe
90+ }
91+ try {
92+ temp_min = day.getAsJsonPrimitive("temp_min").getAsInt();
93+ } catch (ClassCastException e) {
94+ // ignroe
95+ }
96+ try {
97+ pop = day.getAsJsonPrimitive("pop").getAsInt();
98+ } catch (ClassCastException e) {
99+ // ignroe
100+ }
101+
102+ config.saveWeather(index, ident, label, temp_min, temp_max, pop);
103+
104+ index++;
105+ if (index > 2) break;
106+ }
75107
76- index++;
77- if (index > 2) break;
108+ config.saveUpdatedAt();
109+ Log.d("forecast-fetcher", "Forecast has been fetched successfully.");
110+ if (callback != null)
111+ callback.run();
112+ }
78113 }
79114
80- config.saveUpdatedAt();
81- Log.d("forecast-fetcher", "Forecast has been fetched successfully.");
82- return true;
115+ new JsonFetcher(config, callback).execute();
83116 }
84117
85- private JsonObject getJson() {
118+ protected JsonObject getJson() {
119+ URL url = null;
120+ HttpURLConnection httpconn = null;
121+ try {
122+ url = new URL(BASE_URL + areaNs + "/" + areaId + ".json");
123+ } catch (MalformedURLException e) {
124+ Log.e("forecast-fetcher", "BAD URL on getting json: " + e.getMessage());
125+ e.printStackTrace();
126+ return null;
127+ }
86128 try {
87- return Ion.with(this.context)
88- .load(BASE_URL + areaNs + "/" + areaId + ".json")
89- .setHeader("X-Requested-With", "Android cloud.hmml.mmw")
90- .asJsonObject().get();
91- } catch (InterruptedException e) {
92- Log.e("forecast-fetcher", "Exception in fetching json: " + e.getMessage());
129+ httpconn = (HttpURLConnection) url.openConnection();
130+ } catch (IOException e) {
131+ Log.e("forecast-fetcher", "Failed to open connection to get JSON: " + e.getMessage());
93132 e.printStackTrace();
94133 return null;
95- } catch (ExecutionException e) {
96- Log.e("forecast-fetcher", "ExecutionException in fetching json: "+e.getMessage());
134+ }
135+ httpconn.addRequestProperty("X-Requested-With", "Android cloud.hmml.mmw");
136+ httpconn.addRequestProperty("User-Agent", "cloud.hmml.mmw/" + BuildConfig.VERSION_NAME + " (Android " + Build.VERSION.RELEASE + "; " + Build.DEVICE + ")");
137+ try {
138+ httpconn.connect();
139+ } catch (IOException e) {
140+ Log.e("forecast-fetcher", "Failed to request to get JSON: " + e.getMessage());
97141 e.printStackTrace();
98142 return null;
99143 }
144+
145+ try {
146+ try {
147+ int retcode = httpconn.getResponseCode();
148+ if (retcode != 200) {
149+ Log.e("forecast-fetcher", "Invalid return code " + retcode + " on getting JSON");
150+ return null;
151+ }
152+ } catch (IOException e) {
153+ Log.e("forecast-fetcher", "Failed to get response code on getting JSON: " + e.getMessage());
154+ e.printStackTrace();
155+ return null;
156+ }
157+
158+ try {
159+ return (new JsonParser()).parse(new JsonReader(new InputStreamReader(httpconn.getInputStream()))).getAsJsonObject();
160+ } catch (IOException e) {
161+ Log.e("forecast-fetcher", "Failed to read stream data on getting JSON: " + e.getMessage());
162+ e.printStackTrace();
163+ return null;
164+ }
165+ } finally {
166+ httpconn.disconnect();
167+ }
100168 }
101169 }