Android - 等待齐射响应以继续

2024-01-06

我构建了一个应用程序,它在地图中加载标记,我为 volley JSON 文件获取标记,但我需要首先加载 volley,然后继续执行代码,因为另一种方式显示错误纬度空,这个参数加载得不快,另一个方法先执行并显示 null。

我的负载标记齐射代码

public  void getMarkers(){

    requestQueue = Volley.newRequestQueue(getApplicationContext());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {


        @Override
        public void onResponse(JSONObject response) {

            try {

                JSONArray saagTracker = response.getJSONArray("saagMRK");
                for (int i = 0; i < saagTracker.length(); i++) {
                    JSONObject object = saagTracker.getJSONObject(i);

                    title = object.getString(TITLE);
                    snnipet = object.getString(SNNIP);
                    latLng = new LatLng(Double.parseDouble(object.getString(LAT)), Double.parseDouble(object.getString(LNG)));
                    coor = coor + "|" + object.getString(LAT) + "," + object.getString(LNG);                        // Menambah data marker untuk di tampilkan ke google map

                    addMarker(latLng, title, snnipet);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //JSONArray array = new JSONArray(response.body().string());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, "Ocurrio un error", Toast.LENGTH_LONG).show();
        }
    });
    requestQueue.add(jsonObjectRequest);
}

onCreate它首先需要 latLng 才能加载我的geofire范围

geoQuery = geofire.queryAtLocation(new GeoLocation(latLng.latitude, latLng.longitude),0.1f); // latLng it coming null

您需要为齐射响应实施回调。执行此操作的一个非常简单的方法如下。

第1步:创建这个接口

public interface VolleyCallBack {
    void onSuccess();
}

第 2 步:改变你的getMarkers()方法:

public  void getMarkers(final VolleyCallBack callBack){

requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {


    @Override
    public void onResponse(JSONObject response) {

        try {

            JSONArray saagTracker = response.getJSONArray("saagMRK");
            for (int i = 0; i < saagTracker.length(); i++) {
                JSONObject object = saagTracker.getJSONObject(i);

                title = object.getString(TITLE);
                snnipet = object.getString(SNNIP);
                latLng = new LatLng(Double.parseDouble(object.getString(LAT)), Double.parseDouble(object.getString(LNG)));
                coor = coor + "|" + object.getString(LAT) + "," + object.getString(LNG);                        // Menambah data marker untuk di tampilkan ke google map

                addMarker(latLng, title, snnipet);

                callback.onSuccess();

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //JSONArray array = new JSONArray(response.body().string());

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(MainActivity.this, "Ocurrio un error", Toast.LENGTH_LONG).show();
    }
});
requestQueue.add(jsonObjectRequest);
}

第 3 步:致电您的getMarkers()像这样的方法:

getMarkers(new VolleyCallBack() {

                @Override
                public void onSuccess() {
                    // this is where you will call the geofire, here you have the response from the volley.
                    geoQuery = geofire.queryAtLocation(new GeoLocation(latLng.latitude, latLng.longitude),0.1f);
                });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - 等待齐射响应以继续 的相关文章

随机推荐