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_lonnullとして宣言すると、アプリケーションが強制的に閉じられます。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 に答える