简体   繁体   English

Android Studio - 从 Google Places 详细信息 API 解析 JSON

[英]Android Studio - Parsing JSON from Google Places Details API Phone Number giving null on first run

Bit of a strange issue here.这里有点奇怪的问题。 Currently I have an activity which gets the names and place ids of all the taxi companies using Google Places Search API and places them into a list view.目前我有一个活动,它使用 Google Places Search API 获取所有出租车公司的名称和地点 ID,并将它们放入列表视图中。 Then when the item is clicked it will take the place id and run the Places Details API to fetch the phone number.然后,当单击该项目时,它将获取地点 ID 并运行地点详细信息 API 以获取电话号码。

Currently I am running into the issue where the first time the item is clicked in the list view the JSON parser will return a NULL and the phone will dial a number like 6855 or 685-5.目前我遇到的问题是,第一次在列表视图中单击该项目时,JSON 解析器将返回 NULL,电话将拨打 6855 或 685-5 之类的号码。 If you go back and click on the same item or another item then the phone number will fetch correctly.如果您返回 go 并单击同一项目或另一个项目,则电话号码将正确获取。

In the activity I am running 2 JSON parsers however they run at different times.在活动中,我正在运行 2 个 JSON 解析器,但是它们在不同的时间运行。

onCreate:创建:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_taxi);
    mQueue = Volley.newRequestQueue(this);
    pQueue = Volley.newRequestQueue(this);
    listItem = new ArrayList<>();
    placeid = new ArrayList<>();
    police = findViewById(R.id.textView11);
    userlist = findViewById(R.id.users_list);
    jsonParse();
    userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String phone = jsonParseNumber(position);
            }
        });
    }

jsonParse (Gets the names of the taxi companies locally) jsonParse(获取本地出租车公司的名称)

        private void jsonParse() {
        String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.6232817,0.2919483&radius=1000&type=taxi%20service&keyword=taxi&key=APIKEY";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("results");
                for (int i = 0; i < jsonArray.length(); i++){
                    JSONObject result = jsonArray.getJSONObject(i);
                    String name = result.getString("name");
                    String id = result.getString("place_id");
                    placeid.add(id);
                    listItem.add(name);
                }
                adapter = new ArrayAdapter(Taxi.this, R.layout.row, listItem);
                userlist.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mQueue.add(request);
}

jsonParseNumber (gets the number and calls it - This is the code which is returning NULL when it is first ran) jsonParseNumber(获取号码并调用它 - 这是第一次运行时返回 NULL 的代码)

private String jsonParseNumber(int pos) {
    String id = placeid.get(pos);
    String url = "https://maps.googleapis.com/maps/api/place/details/json?placeid="+id+"&key=APIKEY";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject jsonArray = response.getJSONObject("result");
                for (int i = 0; i < jsonArray.length(); i++){
                    number = jsonArray.getString("formatted_phone_number");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    pQueue.add(request);
    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel: " + number));
    startActivity(intent);
    return number;

}

At first I thought it could be that the Dial Action is being called before the Parser finished so I moved it to the end of jsonParseNumber to make sure but it still does the same.起初我认为可能是在 Parser 完成之前调用了 Dial Action,所以我将它移到 jsonParseNumber 的末尾以确保它仍然执行相同的操作。

Quick notes:快速笔记:

2 JSON Parsers in Activity 2 JSON 解析器在活动中

1st Parser gets JSON from GOOGLE PLACES SEARCH API to get names and place id of taxi companies in the area第一个解析器从 GOOGLE PLACES SEARCH API 获取 JSON 以获取该地区出租车公司的名称和地点 ID

2nd Parser uses the place id to get a JSON from GOOGLE PLACES DETAILS to get the phone number第二个解析器使用地点 ID 从 GOOGLE PLACES DETAILS 获取 JSON 以获取电话号码

When the 2nd Parser is ran it will return a NULL on the first run however if called again it will return the return the intended phone number and dial it.当第二个解析器运行时,它会在第一次运行时返回 NULL 但是如果再次调用它将返回预期的电话号码并拨打它。

It is because, your number is null by the time you return "number".这是因为,当您返回“号码”时,您的号码是 null。 You are not waiting until the api call is complete.您不会等到 api 调用完成。 It is better you implement a callback when you get the response to perform specific action.当您获得执行特定操作的响应时,最好实现回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM