首页 > 解决方案 > 如何从 URL 访问我的应用程序中的 JSON 数据?

问题描述

JSON数据

在这个 URL 中,我有一些 Json 数据。如何使用该 URL 将该数据获取到我的 andorid 应用程序。

我在谷歌上看到了参考资料。但没有得到解决方案。

我是 Andorid 的新手。

请帮我。

标签: javaandroidjson

解决方案


做这个 :

步骤 - 1 在您的 gradle 中导入 volley 库:

实施 'com.android.volley:volley:1.1.0'

然后在java中写下这段代码:

    ProgressDialog progressDialog; // define globally

 public void getLocations(){   //call this method onCreate or on OnClickEvent

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Feteching....");
    progressDialog.setCancelable(false);
    progressDialog.show();

    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

    StringRequest request = new StringRequest(Request.Method.GET, "YOUR URL", new Response.Listener<String>() { //you can change here POST/GET
        @Override
        public void onResponse(String response) {
            progressDialog.dismiss();
            System.out.println("Response : " + response);
            try {

                 JSONObject jsonResponse = new JSONObject(response);

                  JSONArray locations = jsonResponse.getJSONArray("LOCATIONS");
                  for (int i = 0; i < locations.length(); i++) {
                  JSONObject jsonObject = locations.getJSONObject(i);

               String name = jSONObject.getString("name");
                String lat = jSONObject.getString("lat");
                String lng = jSONObject.getString("lng");
                 System.out.println("LOCATIONS : " + name +"," + lat + "," + lng);

               // check this print in logcats

                 }
               } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("VolloError " + error);
            progressDialog.dismiss();
            Toast.makeText(YourActivity.this, "Network Connection Error...!!!", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            // use params when you are using POST method

            return params;
        }

    };

    request.setRetryPolicy(new RetryPolicy() {
        @Override
        public int getCurrentTimeout() {
            return 50000;
        }

        @Override
        public int getCurrentRetryCount() {
            return 50000;
        }

        @Override
        public void retry(VolleyError error) throws VolleyError {

        }
    });
    queue.add(request);
}

推荐阅读