我想显示json文件中的标记

时间:2017-04-23 21:41:37

标签: java android json

标记不会在地图上显示,我不明白错误

My json file:[
    {
        "name": "aaaa",
        "lat": 35.747394,
        "lang": 51.267577
    }
]
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    List<Position> positionsList = new ArrayList<>();
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        //delete if static
        PositionsTask positionsTask = new PositionsTask();
        positionsTask.execute();
        // end delete

        // positionsList.add(new Position("beb souika",1,1));
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng TUNIS = new LatLng(36.818248, 10.182416);

        for(Position pos: positionsList){
            LatLng latLng = new LatLng(pos.getLatitude(),pos.getLongitude());
            if(mMap!=null){
                mMap.addMarker(new MarkerOptions().position(latLng).title(pos.getName()));
            }
        }
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(TUNIS,11));
    }

    private class PositionsTask extends AsyncTask<Void, Void, Void> {


        private static final String TAG_ID = "id";
        private static final String TAG_NAME = "name";
        private static final String TAG_LATITUDE = "latitude";
        private static final String TAG_lONGITUDE = "longitude";


        @Override
        protected  void onPreExecute(){
            progressDialog = new ProgressDialog(MapsActivity.this);
            progressDialog.setMessage("Chargement en cours ...");
            progressDialog.show();
        }
        @Override
        protected Void doInBackground(Void... params) {

            String link;
            BufferedReader bufferedReader;
            String result="[]";

            try {


                link = "http://192.168.8.105/map1.php";
                URL url = new URL(link);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String line = null;
                StringBuilder sb = new StringBuilder() ;
                while ((line = bufferedReader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();


            } catch (Exception e) {
                Log.e("Exception: ",e.getMessage());
            }
            positionsList = getPositionList(result);

            return null;
        }

        @Override
        protected void onPostExecute(Void params) {
            progressDialog.dismiss();
        }

        @Override
        protected void onCancelled() {
            progressDialog.dismiss();
        }

        protected List getPositionList(String myJSON){
            List<Position> positions = new ArrayList<>();
            try {
                JSONArray data = new JSONArray(myJSON);

                for(int i=0;i<data.length();i++){
                    Position position = new Position();
                    JSONObject json = data.getJSONObject(i);
                    position.setId(json.getInt(TAG_ID));
                    position.setName(json.getString(TAG_NAME));
                    position.setLatitude(json.getDouble(TAG_LATITUDE));
                    position.setLongitude(json.getDouble(TAG_lONGITUDE));

                    positions.add(position);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return positions;
        }
    }

}
04-23 21:33:16.491 2567-3598/? E/Parcel: Class not found when unmarshalling: com.facebook.search.api.GraphSearchQuery
                                         java.lang.ClassNotFoundException: com.facebook.search.api.GraphSearchQuery
                                             at java.lang.Class.classForName(Native Method)
                                             at java.lang.Class.forName(Class.java:324)
                                             at android.os.Parcel.readParcelableCreator(Parcel.java:2404)
                                             at android.os.Parcel.readParcelable(Parcel.java:2358)
                                             at android.os.Parcel.readValue(Parcel.java:2264)
                                             at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
                                             at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                             at android.os.BaseBundle.getString(BaseBundle.java:920)
                                             at android.content.Intent.getStringExtra(Intent.java:6221)
                                             at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:2751)
                                             at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:2209)
                                             at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:6591)
                                             at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:6346)
                                             at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:170)
                                             at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:4166)
                                             at android.os.Binder.execTransact(Binder.java:453)
                                          Caused by: java.lang.ClassNotFoundException: com.facebook.search.api.GraphSearchQuery
                                             at java.lang.Class.classForName(Native Method)
                                             at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                             at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                             at java.lang.Class.classForName(Native Method) 
                                             at java.lang.Class.forName(Class.java:324) 
                                             at android.os.Parcel.readParcelableCreator(Parcel.java:2404) 
                                             at android.os.Parcel.readParcelable(Parcel.java:2358) 
                                             at android.os.Parcel.readValue(Parcel.java:2264) 
                                             at android.os.Parcel.readArrayMapInternal(Parcel.java:2614) 
                                             at android.os.BaseBundle.unparcel(BaseBundle.java:221) 
                                             at android.os.BaseBundle.getString(BaseBundle.java:920) 
                                             at android.content.Intent.getStringExtra(Intent.java:6221) 
                                             at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:2751) 
                                             at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:2209) 
                                             at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:6591) 
                                             at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:6346) 
                                             at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:170) 
                                             at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:4166) 
                                             at android.os.Binder.execTransact(Binder.java:453) 
                                          Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

0 个答案:

没有答案