如何从该URL获取Json?

时间:2019-04-14 19:27:00

标签: android json url

我正在尝试构建一个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个站点。如果您在Google上尝试使用,则可以看到我正在尝试接收的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 Map上。

0 个答案:

没有答案