【问题标题】:How to retrieve the data from an url using volley which has no array name?如何使用没有数组名称的 volley 从 url 中检索数据?
【发布时间】:2020-06-27 09:22:06
【问题描述】:

图像包含没有数组名称的数据,我需要使用 volley 获取图像中的数据。帮我提供它的代码。

【问题讨论】:

    标签: java android json android-studio mobile-application


    【解决方案1】:

    您必须使用 JSONArrayRequest 。当您解析响应时,它是一个可以使用的 JSONArray 对象。您可以循环数组以按位置获取 JSONObject 项。

    这已在此线程中介绍-> Volley - Sending a POST request using JSONArrayRequest

    【讨论】:

      【解决方案2】:

      String url="https://earthquake.usgs.gov/archive/product/nearby-cities/ci39269503/ci/1593242325224/nearby-cities.json"; JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.GET, url, new Response.Listener() { @覆盖 公共无效 onResponse(JSONArray 响应){ StringBuilder stringBuilder=new StringBuilder();

                  try {
      
                     for (int i=0;i<response.length();i++){
                         JSONObject citiesjsonObject=response.getJSONObject(i);
                         stringBuilder.append("Name : "+citiesjsonObject.getString("name")+
                                 "\n"+"Distance : "+citiesjsonObject.getString("distance")+ "\n"
                                 +"Population : "+citiesjsonObject.getString("population"));
      
      
                         stringBuilder.append("\n\n");
                     }
      
                      
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
      
      
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
      
              }
          });
          requestQueue.add(jsonArrayRequest);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-27
        • 1970-01-01
        • 2020-08-03
        • 2021-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多