-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/example/quakereport/EarthquakeLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.example.quakereport; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.loader.content.AsyncTaskLoader; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 通过使用 AsyncTask 执行 | ||
* 给定 URL 的网络请求,加载地震列表。 | ||
*/ | ||
public class EarthquakeLoader extends AsyncTaskLoader<List<Earthquake>> { | ||
|
||
/** | ||
* 日志消息标签 | ||
*/ | ||
private static final String LOG_TAG = EarthquakeLoader.class.getName(); | ||
|
||
/** | ||
* 查询 URL | ||
*/ | ||
private String url; | ||
|
||
/** | ||
* 构建新 {@link EarthquakeLoader}。 | ||
* <p> | ||
* 活动的 @param 上下文 | ||
* 要从中加载数据的 @param url | ||
*/ | ||
public EarthquakeLoader(Context context, String url) { | ||
super(context); | ||
this.url = url; | ||
} | ||
|
||
@Override | ||
protected void onStartLoading() { | ||
forceLoad(); | ||
} | ||
|
||
/** | ||
* 这位于后台线程上。 | ||
*/ | ||
@Override | ||
public List<Earthquake> loadInBackground() { | ||
if (null == url) { | ||
return null; | ||
} | ||
|
||
// 执行网络请求、解析响应和提取地震列表。 | ||
List<Earthquake> earthquakes = QueryUtils.fetchEarthquakeData(url); | ||
return earthquakes; | ||
} | ||
} |