0

我正在尝试构建一个 Android 应用程序,我在地图上显示不同的自行车站,并在城市周围使用标记。我正在使用市政府提供的公共 API。(http://www.zaragoza.es/docs-api_sede/#!/Equipamientos_y_movilidad%3A_Estaciones_Bizi/get_servicio_urbanismo_infraestructuras_estacion_bicicleta)。

我的问题是我不知道如何将这个 JSON 接收到我的应用程序中。我尝试了不同的方法,但我总是遇到 InputStream 异常,说没有文件。我正在使用此 URL 进行测试:(https://www.zaragoza.es/sede/servicio/urbanismo-infraestructuras/equipamiento/aparcamiento-bicicleta?rf=html&srsname=wgs84&rows=2&distance=5000)使用此 URL,您只需2个车站。如果你在谷歌上尝试,你可以看到我试图接收的 JSON,它是这样的:

{"totalCount":1122,"start":0,"rows":2,"result":[{"id":293,"title":"CALLE SOBRARBE, 46","tipo":"Abierto","plazas":6,"anclajes":3,"lastUpdated":"2018-12-24T00:00:00Z","geometry":{"type":"Point","coordinates":[-0.873314510445586,41.66099094543581]},"icon":"//www.zaragoza.es/contenidos/iconos/aparcabici.png"},{"id":294,"title":"AVENIDA SAN JUAN DE LA PEÑA, 1","tipo":"Abierto","plazas":10,"anclajes":5,"lastUpdated":"2018-12-24T00:00:00Z","geometry":{"type":"Point","coordinates":[-0.8732431572812822,41.66172067640118]},"icon":"//www.zaragoza.es/contenidos/iconos/aparcabici.png"}]}

这是我试图接收 JSON 的片段。

public class fMap extends Fragment implements OnMapReadyCallback {

    String page= "https://www.zaragoza.es/sede/servicio/urbanismo-infraestructuras/equipamiento/aparcamiento-bicicleta?rf=html&srsname=wgs84&rows=2&distance=5000";

public fMap() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_f_map, container, false);

        new JsonTask().execute("page");

        return v;
    }



    private class JsonTask extends AsyncTask<String, String, String> {

        protected void onPreExecute() {
            super.onPreExecute();

            pd = new ProgressDialog(getContext());
            pd.setMessage("Please wait");
            pd.setCancelable(false);
            pd.show();
        }

        protected String doInBackground(String... params) {


            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();


                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {
                    buffer.append(line+"\n");
                    Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

                }

                return buffer.toString();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        } 
}
}

我的目标是获取这个 JSON,这样我就可以获取车站并将它们显示在 Google 地图上。

4

0 回答 0