0

这是我的代码

{ 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;
}



   }
   }

当我删除评论时遇到问题

 //System.out.println("Testingggg..." + json.length());

当我删除评论时

 /*
  try{
   place = json.getJSONArray(TAG_PLACE);
   for(int i=0;i<place.length();i++){
    JSONObject c = place.getJSONObject(i); .....

我尝试了很多我没有成功的问题是解析json并感谢每一个人

这是我的 json 文件

           {"place":    [{"id":"1","name_place":"Jedeida","latitude":"36.502234","longitude":"9.561264","person":"ahmed","date":"2012-08-01","time":"07:45:50"},
{"id":"2","name_place":"jedeida","latitude":"36.502234","longitude":"9.561109","person":"ahmed","date":"2012-08-01","time":"07:46:30"},{"id":"3","name_place":"jedeida","latitude":"36.501676","longitude":"9.562135","person":"ahmed","date":"2012-08-01","time":"07:48:03"},{"id":"4","name_place":"jedeida","latitude":"36.50083","longitude":"9.563069","person":"ahmed","date":"2012-08-01","time":"07:50:05"},{"id":"5","name_place":"Tebourba","latitude":"36.51029","longitude":"9.553043","person":"ali","date":"2012-08-02","time":"06:05:41"},{"id":"6","name_place":"jedeida","latitude":"36.504886","longitude":"9.553918","person":"ali","date":"2012-08-02","time":"06:07:04"},{"id":"7","name_place":"jedeida","latitude":"36.503503","longitude":"9.555477","person":"ali","date":"2012-08-02","time":"06:09:23"},{"id":"8","name_place":"jedeida","latitude":"36.50211","longitude":"9.561287","person":"ali","date":"2012-08-02","time":"06:11:40"},{"id":"9","name_place":"jedeida","latitude":"36.501208","longitude":"9.562281","person":"ali","date":"2012-08-02","time":"06:13:26"},{"id":"10","name_place":"jedeida","latitude":"36.500477","longitude":"9.563136","person":"ali","date":"2012-08-02","time":"06:15:01"}]}

这是 logcat 文件的 链接 在此处输入链接描述

4

3 回答 3

1

您将字符串数组p_latp_lon 声明null,这就是您的应用程序被强制关闭的原因。您的 JSON 解析代码运行良好。

您必须初始化具有特定大小的字符串数组,如下所示。

String[] p_lat= new String[10];
String[] p_lon= new String[10];

您也可以使用ArrayList以获得更好的性能。

于 2012-09-05T15:05:53.553 回答
0

我正在使用 wampserver,看来我必须改变

private static String url = "http://localhost/test/json_parser.php";

private static String url = "http://10.0.2.2/test/json_parser.php";

问题似乎是由不正确的地址引起的,这就是为什么当您遇到问题时不要忘记使用log.d("","");

于 2012-09-05T23:24:21.147 回答
0
JSONObject c = place.getJSONObject(i);

您确定每个索引 (i) 都是 jsonobject 而不是其他值/字符串吗?

System.out.println("Testingggg..." + json.length());

尝试 Integer.toString(json.length())

于 2012-09-05T14:48:58.103 回答