AsyncTask doInBackground()在运行时无法正确执行,但适用于调试器

时间:2017-10-31 22:08:37

标签: arraylist

@Override
    protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
        ArrayList<HashMap<String, String>> PLIST = new ArrayList<>();
        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(jsonUrl);
        ArrayList<String> URLList = new ArrayList<>();

        if (jsonStr != null) {
            placesList.clear();
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                // Getting JSON Array node
                JSONArray placesJsonArray = jsonObj.getJSONArray("results");
                String pToken = "";

                // looping through All Places
                for (int i = 0; i < placesJsonArray.length(); i++) {
                    JSONObject placesJSONObject = placesJsonArray.getJSONObject(i);

                    String id = placesJSONObject.getString("id");
                    String name = placesJSONObject.getString("name");

                    HashMap<String, String> places = new HashMap<>();

                    // adding each child node to HashMap key => value
                    places.put("id", id);
                    places.put("name", name);

                    PLIST.add(places);
                }

                //TODO: fix this...
                if (SEARCH_RADIUS == 1500) {
                    Log.e(TAG, "did it get to 1500?");
                    try {
                        for (int k = 0; k < 2; k++) {
                            //error is no value for next_page_token... this

错误

                            pToken = jsonObj.getString("next_page_token"); //if I place breakpoint here, debugger runs correctly, and returns more than 20 results if there is a next_page_token.
                            String newjsonUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="
                                    + midpointLocation.getLatitude() + "," + midpointLocation.getLongitude()
                                    + "&radius=" + SEARCH_RADIUS + "&key=AIzaSyCiK0Gnape_SW-53Fnva09IjEGvn55pQ8I&pagetoken=" + pToken;
                            URLList.add(newjsonUrl);

                            jsonObj = new JSONObject(new HttpHandler().makeServiceCall(newjsonUrl)); //moved

                            Log.e(TAG, "page does this try catch");
                            }
                        }

                    catch (Exception e ) {
                        Log.e(TAG, "page token not found: " + e.toString());
                    }
                    for (String url : URLList){
                        Log.e(TAG, "url is : " + url);
                    }

在多次尝试调试此代码后,我创建了一个URLS的ArrayList,我计划在添加了next_page_tokens的所有url之后解压缩ArrayList,然后再解析每个url。当在pToken = getString(&#34; next_page_token&#34;)上运行带有断点的调试器时,我从Logger获取第一个url,然后正确地获取第二个url。当我按原样运行时,我得到第一个url,然后出现以下错误: JSONException:next_page_token没有值

我尝试过的事情

使缓存无效并重新启动

清洁构建

在不同的SDK版本上运行

确保if语句正在命中(SEARCH_RADIUS == 1500)

非常感谢任何帮助,谢谢!

在这样的侦听器函数中调用函数。

new GetPlaces(new AsyncResponse() {
        @Override
        public void processFinish(ArrayList<HashMap<String, String>> output) {
            Log.e(TAG, "outputasync:" );
            placesList = output;
        }
    }).execute();

我的onPostExecute方法。

@Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        delegate.processFinish(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

1 个答案:

答案 0 :(得分:0)

事实证明,google放置api需要几毫秒才能生成next_page_token。因此,我使用wait()函数在创建基于next_page_token的新生成的url之前暂停。这解决了我的问题。谢谢你的帮助。