Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
Expand All @@ -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'
}
44 changes: 34 additions & 10 deletions app/src/main/java/com/example/android/soonami/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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));
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +20,10 @@ buildscript {
allprojects {
repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
}

Expand Down