Skip to content

Commit

Permalink
从 USGS 请求数据并展示
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Jan 24, 2021
1 parent b14692a commit f7ed29a
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 53 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.quakereport">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
93 changes: 79 additions & 14 deletions app/src/main/java/com/example/quakereport/EarthquakeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,118 @@

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class EarthquakeActivity extends AppCompatActivity {

public static final String LOG_TAG = EarthquakeActivity.class.getName();

/**
* URL for earthquake data from the USGS dataset
*/
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=20";
/**
* 地震列表的适配器
*/
private EarthquakeAdapter earthquakeAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.earthquake_activity);

// Create a fake list of earthquake locations.
ArrayList<Earthquake> earthquakes = QueryUtils.extractEarthquakes();

// Find a reference to the {@link ListView} in the layout
// 在布局中查找 {@link ListView} 的引用
ListView earthquakeListView = (ListView) findViewById(R.id.list);

// Create a new {@link ArrayAdapter} of earthquakes
final EarthquakeAdapter adapter = new EarthquakeAdapter(this, earthquakes);
// 创建新适配器,将空地震列表作为输入
earthquakeAdapter = new EarthquakeAdapter(this, new ArrayList<Earthquake>());

// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
earthquakeListView.setAdapter(adapter);
// {@link ListView} 上设置适配器
// 以便可以在用户界面中填充列表
earthquakeListView.setAdapter(earthquakeAdapter);

// 在 ListView 上设置项目单击监听器,该监听器会向 Web 浏览器发送 intent,
// 打开包含有关所选地震详细信息的网站。
earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// 查找单击的当前地震
Earthquake currentEarthquake = adapter.getItem(position);
Earthquake currentEarthquake = earthquakeAdapter.getItem(position);

// 将字符串 URL 转换为 URI 对象(以传递至 Intent 中 constructor)
// 将字符串 URL 转换成 URI 对象(传递到 Intent 构造函数中)
Uri earthquakeUri = Uri.parse(currentEarthquake.getUrl());

// 创建一个新的 Intent 以查看地震 URI
// 创建新 intent 以查看地震 URI
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, earthquakeUri);

// 发送 Intent 以启动新活动
// 发送 intent 以启动新活动
startActivity(websiteIntent);
}
});

// 启动 AsyncTask 以获取地震数据
EarthquakeAsyncTask task = new EarthquakeAsyncTask();
task.execute(USGS_REQUEST_URL);
}

/**
* {@link AsyncTask} 用于在后台线程上执行网络请求,然后
* 使用响应中的地震列表更新 UI。
* <p>
* AsyncTask 有三个泛型参数:输入类型、用于进度更新的类型和
* 输出类型。我们的任务将获取字符串 URL 并返回地震。我们不会执行
* 进度更新,因此第二个泛型是无效的。
* <p>
* 我们将仅覆盖 AsyncTask 的两个方法:doInBackground() 和 onPostExecute()。
* doInBackground() 方法会在后台线程上运行,因此可以运行长时间运行的代码
* (如网络活动),而不会干扰应用的响应性。
* onPostExecute() 在 UI 线程上运行,系统会将 doInBackground() 方法的结果传递给它,
* 因此该方法可使用生成的数据更新 UI。
*/
private class EarthquakeAsyncTask extends AsyncTask<String, Void, List<Earthquake>> {
/**
* 此方法在后台线程上运行并执行网络请求。
* 我们不能够通过后台线程更新 UI,因此我们返回
* {@link Earthquake} 的列表作为结果。
*/
@Override
protected List<Earthquake> doInBackground(String... urls) {
// 如果不存在任何 URL 或第一个 URL 为空,切勿执行请求。
if (urls.length < 1 || urls[0] == null) {
return null;
}

List<Earthquake> earthquakes = QueryUtils.fetchEarthquakeData(urls[0]);
return earthquakes;
}

/**
* 后台工作完成后,此方法会在主 UI 线程上
* 运行。此方法接收 doInBackground() 方法的返回值
* 作为输入。首先,我们将清理适配器,除去先前 USGS 查询的地震
* 数据。然后,我们使用新地震列表更新适配器,
* 这将触发 ListView 重新填充其列表项。
*/
@Override
protected void onPostExecute(List<Earthquake> earthquakes) {
// 清除之前地震数据的适配器
earthquakeAdapter.clear();

// 如果存在 {@link Earthquake} 的有效列表,则将其添加到适配器的
// 数据集。这将触发 ListView 执行更新。
if (!earthquakes.isEmpty() && null != earthquakes) {
earthquakeAdapter.addAll(earthquakes);
}
}
}
}

0 comments on commit f7ed29a

Please sign in to comment.