我正在开发一个调用远程服务器获取数据的项目。服务器提供纬度,经度和一些信息。我可以调用服务器并获得响应。但我没有得到如何从该响应中提取数据并在地图上绘制标记。
这是我的代码
public class NearActivity extends Activity实现LocationListener {
GoogleMap gMap;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_near);
gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
new HttpGetTask().execute();
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
location.getLatitude(), location.getLongitude()), 14));
gMap.addMarker(new MarkerOptions()
.position(
new LatLng(location.getLatitude(), location
.getLongitude())).title("I'm here"));
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.near, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private class HttpGetTask extends AsyncTask<Void, Void, String> {
Double lat = locationManager.getLastKnownLocation(provider)
.getLatitude();
Double lng = locationManager.getLastKnownLocation(provider)
.getLongitude();
String URL = "http:/?xyz.around_me.json?app_id=test&lat="
+ lat + "&lng=" + lng;
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected String doInBackground(Void... params) {
HttpGet request = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (null != mClient)
mClient.close();
Log.v("Response", result);
// Toast.makeText(getBaseContext(), result,
// Toast.LENGTH_SHORT).show();
try {
JSONArray json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
JSONObject e = json.getJSONObject(i);
String point = e.getString("point");
Log.v("POINT", point);
String[] point2 = point.split(",");
double lat1 = Double.parseDouble(point2[0]);
double lng1 = Double.parseDouble(point2[1]);
Log.v("LLDN", ""+lat1+"&"+lng1);
gMap.addMarker(new MarkerOptions().title(e
.getString("name")).position(new LatLng(lat1, lng1)));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是我的json回复
[
{
point: "77.606706,12.967060",
distance: "1.41",
id: 5686,
ph: "1234",
name: "zxcvb",
LS: " asdfrewrtet"
},
{
point: "77.606706,12.967060",
distance: "1.41",
id: 5686,
ph: "1234",
name: "zxcvb",
LS: " asdfrewrtet"
},
{
point: "77.606706,12.967060",
distance: "1.41",
id: 5686,
ph: "1234",
name: "zxcvb",
LS: " asdfrewrtet"
},
]
有人帮我从这个回复中提取数据。 如果我提取数据,我可以放置标记。
答案 0 :(得分:2)
您需要从JSON对象获取数据,类似于以下工作示例:
@Override
protected void onPostExecute(String result) {
// Had to create this hard coded JSON result string to have something to test with.
result = "[ { " +
"\"point\":\"77.606706,12.967060\"," +
"\"name\":\"MyMarker\"}]";
try {
JSONArray json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
JSONObject e = json.getJSONObject(i);
String point = e.getString("point");
String[] point2 = point.split(",");
double lat1 = Double.parseDouble(point2[0]);
double lng1 = Double.parseDouble(point2[1]);
gMap.addMarker(new MarkerOptions().title(e.getString("name")).position(new LatLng(lat1, lng1)));
}
} catch (JSONException e) {
e.printStackTrace();
}
}