我有一个有点长的问题。我现在正在学习 Google Maps API,我需要让应用程序从网站获取一些 JSON 数据,然后在信息窗口中将其中一些数据设置为标题或其他内容。重要的部分是我必须使用接口类来执行此操作,而下载此数据的 AsyncTask 也必须在另一个类中。所以我目前非常困惑如何使这种通信成为可能,以及在 AsyncTask 类中尽可能少的代码。我也不是很了解这个界面的东西。请有人帮忙。所以这里是主要活动:
package com.example.gmapsexample;
public class MainActivity extends Activity {
private GoogleMap mMap;
Marker marker = null;
HttpClient klient;
String data;
String s;
Double latt, lngg;
GoogleMapOptions options;
String adres = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=en&username=sartheris";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
options = new GoogleMapOptions();
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
options.mapType(GoogleMap.MAP_TYPE_SATELLITE).compassEnabled(true).rotateGesturesEnabled(true).tiltGesturesEnabled(true).zoomControlsEnabled(true);
klient = new DefaultHttpClient();
GetJson gg = new GetJson();
gg.execute();
/** On Info Window Click */
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
marker.setTitle(s);
Toast.makeText(getApplicationContext(), "Info Window Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
这是 GetJson 类
package com.example.gmapsexample;
public class GetJson extends AsyncTask<String, Void, String> {
HttpClient klient;
String adres = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=en&username=sartheris";
String s;
Double latt, lngg;
private GoogleMap mMap;
Marker marker;
GoogleMapOptions options;
@Override
protected String doInBackground(String... params) {
klient = new DefaultHttpClient();
options = new GoogleMapOptions();
StringBuilder url = new StringBuilder(adres);
HttpGet hg = new HttpGet(url.toString());
HttpResponse hr = null;
try {
hr = klient.execute(hg);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
int status = hr.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity he = hr.getEntity();
String data = null;
try {
data = EntityUtils.toString(he);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonGet = null;
try {
jsonGet = new JSONObject(data);
} catch (JSONException e1) {
e1.printStackTrace();
}
if (jsonGet.has("geonames")) {
try {
JSONObject arrGet = jsonGet.getJSONArray("geonames").getJSONObject(0);
s = arrGet.getString("toponymName");
latt = arrGet.getDouble("lat");
lngg = arrGet.getDouble("lng");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
marker = new Marker(this);
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latt, lngg))
.title("HELLO WORLD").snippet("BLQ")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.draggable(true));
}
}
现在我需要一个接口类来帮助数据传递......