在android上解析Json
这是我的密码在android上解析Json,android,json,google-maps,Android,Json,Google Maps,这是我的密码 { public class MainActivity extends MapActivity { private MapView mapView; //test start private static String url = "http://localhost/test/json_parser.php"; private static final String TAG_PLACE="place"; private static final String TAG_ID="id
{ public class MainActivity extends MapActivity {
private MapView mapView;
//test start
private static String url = "http://localhost/test/json_parser.php";
private static final String TAG_PLACE="place";
private static final String TAG_ID="id";
private static final String TAG_NAME_PLACE="name_place";
private static final String TAG_LATITUDE="latitude";
private static final String TAG_LONGITUDE="longitude";
private static final String TAG_PERSON="person";
private static final String TAG_DATE="date";
private static final String TAG_TIME="time";
JSONArray place = null;
String[] p_lat=null;
String[] p_lon=null;
String id;
String name_place;
String latitude;
String longitude;
String person;
String date;
String time;
//test end
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView=(MapView)findViewById(R.id.mapview);
Drawable marker=getResources().getDrawable(R.drawable.marker);
marker.setBounds((int)(-marker.getIntrinsicWidth()/2),-marker.getIntrinsicHeight(), (int)(marker.getIntrinsicWidth()/2),0);
InterestingLocations funPlaces=new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt=funPlaces.getCenterPt();
int latSpan=funPlaces.getLatSpanE6();
int lonSpan=funPlaces.getLonSpanE6();
Log.v("Overlays","Lat span is" + latSpan);
Log.v("Overlays","Lon span is" + lonSpan);
MapController mc=mapView.getController();
mc.setCenter(pt);
mc.zoomToSpan((int)(latSpan*1.5),(int)(lonSpan*1.5));
//test start
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
//System.out.println("Testingggg..." + json.length());
//test end
//test start
/*
try{
place = json.getJSONArray(TAG_PLACE);
for(int i=0;i<place.length();i++){
JSONObject c = place.getJSONObject(i);
id = c.getString(TAG_ID);
name_place = c.getString(TAG_NAME_PLACE);
latitude = c.getString(TAG_LATITUDE);
//p_lat[i]=latitude;
longitude = c.getString(TAG_LONGITUDE);
//p_lon[i]=longitude;
person = c.getString(TAG_PERSON);
date = c.getString(TAG_DATE);
time = c.getString(TAG_TIME);
}
}catch(JSONException e){
e.printStackTrace();
}/*
System.out.print(p_lat[0]);
System.out.print(p_lon[0]);
*/
//test end
}
public void myClickHandler(View target){
switch(target.getId()){
case R.id.zoomin:
mapView.getController().zoomIn();
break;
case R.id.zoomout:
mapView.getController().zoomOut();
break;
case R.id.sat:
mapView.setSatellite(true);
break;
case R.id.street:
mapView.setStreetView(true);
break;
case R.id.traffic:
mapView.setTraffic(true);
break;
case R.id.normal:
mapView.setSatellite(false);
mapView.setStreetView(false);
mapView.setTraffic(false);
break;
}
mapView.postInvalidateDelayed(2000);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
}
当我从中删除评论时
/*
try{
place = json.getJSONArray(TAG_PLACE);
for(int i=0;i<place.length();i++){
JSONObject c = place.getJSONObject(i); .....
这是到logcat文件的链接
您确定每个索引(i)都是jsonobject而不是其他值/字符串吗
System.out.println("Testingggg..." + json.length());
尝试Integer.toString(json.length())将字符串数组p_lat&p_lon声明为null,这就是应用程序被强制关闭的原因。您的JSON解析代码工作正常 您必须使用如下所示的特定大小初始化字符串数组
String[] p_lat= new String[10];
String[] p_lon= new String[10];
您还可以使用阵列列表以获得更好的性能。我正在使用wampserver,似乎我必须更改
private static String url = "http://localhost/test/json_parser.php";
到
这个问题似乎是由地址不正确引起的
这就是为什么当您遇到问题时不要忘记使用
log.d(“,”)代码>应用程序意外关闭打开开关名称路径“/sys/class/switch/test”时出错没有这样的文件或目录您是否正确获取json字符串?当我在//System.out.println(“Testingggg…”+json.length()中删除“/”时,它会阻塞;即使使用Integer.toString(json.length()),问题仍然存在
System.out.println("Testingggg..." + json.length());
String[] p_lat= new String[10];
String[] p_lon= new String[10];
private static String url = "http://localhost/test/json_parser.php";
private static String url = "http://10.0.2.2/test/json_parser.php";