From d6ac13ed7572e18522178f16276c6983015c3588 Mon Sep 17 00:00:00 2001 From: Amirhossein Arbab Date: Mon, 16 Dec 2019 23:54:19 +0330 Subject: [PATCH 1/3] Update build.gradle --- app/build.gradle | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 52452a2..9c33c34 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 29 defaultConfig { applicationId "com.example.android.soonami" minSdkVersion 15 - targetSdkVersion 23 + targetSdkVersion 29 versionCode 1 versionName "1.0" } @@ -20,7 +19,7 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.4.0' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'androidx.appcompat:appcompat:1.1.0' } From bac690bfe84101008d30a6a8404970357a691259 Mon Sep 17 00:00:00 2001 From: Amirhossein Arbab Date: Mon, 16 Dec 2019 23:58:01 +0330 Subject: [PATCH 2/3] Update build.gradle --- build.gradle | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 03bced9..6beaa09 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,14 @@ buildscript { repositories { jcenter() + maven { + url 'https://maven.google.com/' + name 'Google' + } + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:3.5.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +20,10 @@ buildscript { allprojects { repositories { jcenter() + maven { + url 'https://maven.google.com/' + name 'Google' + } } } From 2496691935f12d21df8d1a41b195ddec9c3db877 Mon Sep 17 00:00:00 2001 From: Amirhossein Arbab Date: Tue, 17 Dec 2019 01:07:41 +0330 Subject: [PATCH 3/3] Update MainActivity.java Import required classes for AndroidX migration --- .../example/android/soonami/MainActivity.java | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/example/android/soonami/MainActivity.java b/app/src/main/java/com/example/android/soonami/MainActivity.java index da73260..ce32c9a 100644 --- a/app/src/main/java/com/example/android/soonami/MainActivity.java +++ b/app/src/main/java/com/example/android/soonami/MainActivity.java @@ -17,10 +17,12 @@ import android.os.AsyncTask; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; +import android.text.TextUtils; import android.util.Log; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -40,12 +42,16 @@ */ public class MainActivity extends AppCompatActivity { - /** Tag for the log messages */ + /** + * Tag for the log messages + */ public static final String LOG_TAG = MainActivity.class.getSimpleName(); - /** URL to query the USGS dataset for earthquake information */ + /** + * URL to query the USGS dataset for earthquake information + */ private static final String USGS_REQUEST_URL = - "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6"; + "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2014-01-01&endtime=2014-12-01&minmagnitude=7"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -62,15 +68,15 @@ protected void onCreate(Bundle savedInstanceState) { */ private void updateUi(Event earthquake) { // Display the earthquake title in the UI - TextView titleTextView = (TextView) findViewById(R.id.title); + TextView titleTextView = findViewById(R.id.title); titleTextView.setText(earthquake.title); // Display the earthquake date in the UI - TextView dateTextView = (TextView) findViewById(R.id.date); + TextView dateTextView = findViewById(R.id.date); dateTextView.setText(getDateString(earthquake.time)); // Display whether or not there was a tsunami alert in the UI - TextView tsunamiTextView = (TextView) findViewById(R.id.tsunami_alert); + TextView tsunamiTextView = findViewById(R.id.tsunami_alert); tsunamiTextView.setText(getTsunamiAlertString(earthquake.tsunamiAlert)); } @@ -154,6 +160,11 @@ private URL createUrl(String stringUrl) { */ private String makeHttpRequest(URL url) throws IOException { String jsonResponse = ""; + + // If the url is null, then return early. + if (url == null) { + return jsonResponse; + } HttpURLConnection urlConnection = null; InputStream inputStream = null; try { @@ -162,10 +173,19 @@ private String makeHttpRequest(URL url) throws IOException { urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); urlConnection.connect(); - inputStream = urlConnection.getInputStream(); - jsonResponse = readFromStream(inputStream); + + //If the request was successful(response code 200) + // then read the input stream and parse the response. + if (urlConnection.getResponseCode() == 200) { + inputStream = urlConnection.getInputStream(); + jsonResponse = readFromStream(inputStream); + } else { + Log.e(LOG_TAG, "Error response code" + urlConnection.getResponseCode()); + } + } catch (IOException e) { - // TODO: Handle the exception + Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results", e); + } finally { if (urlConnection != null) { urlConnection.disconnect(); @@ -201,6 +221,10 @@ private String readFromStream(InputStream inputStream) throws IOException { * about the first earthquake from the input earthquakeJSON string. */ private Event extractFeatureFromJson(String earthquakeJSON) { + // If the JSON string is empty or null, then return early. + if (TextUtils.isEmpty(earthquakeJSON)) { + return null; + } try { JSONObject baseJsonResponse = new JSONObject(earthquakeJSON); JSONArray featureArray = baseJsonResponse.getJSONArray("features");