gpt4 book ai didi

java - 我如何从 wunderground API 获取数据并在 Android 中显示在屏幕上?

转载 作者:行者123 更新时间:2023-11-30 02:47:58 24 4
gpt4 key购买 nike

目前我在 Android Studio 中有一张 map 图片,我想知道如何从 wunderground 中提取信息并将其显示在每个位置的 map 顶部。

This是带有一些示例代码的网站。

最佳答案

这取决于数据的类型。考虑一个从 JSON 提要中提取地震数据位置的示例。

try {
JSONObject object = (JSONObject) new JSONTokener(JSONResponse)
.nextValue();
JSONArray earthquakes = object.getJSONArray("features");

for (int i = 0; i < earthquakes.length(); i++) {
JSONObject tmp = (JSONObject) earthquakes.get(i);
//Log.i("JSON: ",tmp.toString());

JSONObject geometry = tmp.getJSONObject("geometry");
JSONArray coords = geometry.getJSONArray("coordinates");
JSONObject properties = tmp.getJSONObject("properties");

//Log.i("Data", "Coords:"+coords.getString(0) + " "+ coords.getString(1)+"\n Place:"+properties.getString("place")+ " Mag:"+properties.getString("mag"));

if(coords.getString(0) != "" && coords.getString(1) != ""){
result.add(new EarthQuakeRec(
Float.parseFloat(coords.getString(1)),//Lat
Float.parseFloat(coords.getString(0)),//Long
Float.parseFloat(properties.getString("mag")),//Magnitude
properties.getString("place")
)
);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

JSON 结构看起来像这样:

{
"id": "nc72241526",
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-122.0102,
37.6053,
6
]
},
"properties": {
"detail": "http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72241526.geojson",
"type": "earthquake",
"net": "nc",
"tsunami": null,
"sources": ",nc,",
"title": "M 3.0 - 1km NE of Union City, California",
"time": 1403371328500,
"updated": 1403374699020,
"mag": 3,
"types": ",dyfi,focal-mechanism,general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,tectonic-summary,",
"place": "1km NE of Union City, California",
"status": "AUTOMATIC",
"ids": ",nc72241526,",
"alert": null,
"rms": 0.17,
"code": "72241526",
"url": "http://earthquake.usgs.gov/earthquakes/eventpage/nc72241526",
"magType": "Md",
"mmi": null,
"cdi": 3.8,
"tz": -420,
"felt": 319,
"nst": 75,
"dmin": 0.03593261,
"gap": 25.2,
"sig": 260
}

然后您将从 JSONObject 类开始,向下钻取到感兴趣的数据点。使用 Log 类可以帮助您解决问题并衡量遍历数据结构的进度。在我的示例中,它被注释掉了。

对于 XML,您可以尝试一些类似的东西:

        try {

// Create the Pull Parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();

// Set the Parser's input to be the XML document in the HTTP Response
xpp.setInput(new InputStreamReader(response.getEntity()
.getContent()));

// Get the first Parser event and start iterating over the XML document
int eventType = xpp.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {

if (eventType == XmlPullParser.START_TAG) {
startTag(xpp.getName());
} else if (eventType == XmlPullParser.END_TAG) {
endTag(xpp.getName());
} else if (eventType == XmlPullParser.TEXT) {
text(xpp.getText());
}
eventType = xpp.next();
}
return mResults;
} catch (XmlPullParserException e) {
e.printStackTrace();
}

有很多东西可以扔给你,但你没有给出太多的具体细节。希望有所帮助。 ;)

关于java - 我如何从 wunderground API 获取数据并在 Android 中显示在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24707473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com