【问题标题】:Android Java - get place name from json object responseAndroid Java - 从json对象响应中获取地名
【发布时间】:2020-07-28 09:51:58
【问题描述】:

如何在 android 应用程序的字符串地名中获取值“A Salam St, Sib, Oman”。 json 响应附加为图像。请检查 {

String URL="https://maps.googleapis.com/maps/api/geocode/json?latlng="+myLat+","+myLong+"&&key=XXX";
                RequestQueue requestQueue= Volley.newRequestQueue(MercahntUpdate.this);
                //etMID.setText("");
                JsonObjectRequest objectRequest=new JsonObjectRequest(
                        Request.Method.GET, URL, null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                Log.e("success",response.toString());
                                    String loudScreaming = response.optJSONArray("results[0].address_components.formatted_address").toString();

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("error",error.toString());


                            }
                        }
                );

【问题讨论】:

  • 首先使用密钥"results"response获取JSONArray。然后从该数组的 0 索引中获取 JSONObject。然后使用密钥"formatted_address"JSONObject 访问所需的String

标签: android json api android-volley geocode


【解决方案1】:

将原始 Json 放入 http://www.jsonschema2pojo.org/ 并创建一个类来处理序列化。然后您就可以从 getter 访问 formatted_address

下面的伪代码将进入您的onResponse

for(Result res : response.results){
res.getFormattedAddress();
}

【讨论】:

  • 没有其他方法可以直接获取详细信息吗?
猜你喜欢
  • 2013-08-07
  • 1970-01-01
  • 2020-11-14
  • 2014-10-09
  • 2017-06-01
  • 1970-01-01
  • 2015-02-12
  • 2017-04-22
  • 1970-01-01
相关资源
最近更新 更多