Volley StringRequest OnResponse由旧值触发

时间:2019-07-14 17:46:38

标签: android android-volley

我正在制作一个使用location参数的URL StringRequest,我从 second getLocation调用的onSuccess内部将stringRequest添加到队列中。 (我有2个调用getLocationPermissions,一个来自onCreate,仅更新位置参数,另一个来自onResume,它也创建url stringRequest) 调用OnCreate时,我先请求位置权限,然后调用OnResume并创建url stringRequest并使用上一个位置获取我的信息,然后将应用程序移至后台,进行物理移动(以更改位置),然后恢复至该应用程序,调用OnResume,从 second getLocation调用(onResume)更新新位置,然后根据新位置创建 new url stringRequest,但不知道如何以及为什么,我看到被触发的onRespond收到了旧网址的响应!!!是我第一次打开该应用程序时创建的! 为什么?!

我尝试添加:                         myQueue.getCache()。invalidate(url,true); 和                         myQueue.getCache()。clear(); 但这没有帮助

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        myQueue = Volley.newRequestQueue(this);
        setContentView(R.layout.activity_maps);
        // Get the SupportMapFragment and request notification
        // when the map is ready to be used.
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
  LocationServices.getFusedLocationProviderClient(this);
        handleUserLocation();
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    void handleUserLocation(){
        //FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        //check location permisions
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //ask for location permisions
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);
            //return;
        }else {
            //already have permissions
            mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    wayLongitude = location.getLongitude();
                    wayLatitude = location.getLatitude();
                    CheckUserStatus();
                }
            });

        }
    }

@SuppressLint("MissingPermission")
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCESS_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                        @Override
                        public void onSuccess(Location location) {
                            wayLongitude = location.getLongitude();
                            wayLatitude = location.getLatitude();
                        }
                    });
                } else {
                    // permission denied, boo!
                    return;
                }

            }



private void CheckUserStatus() {

        textView = (TextView) findViewById(R.id.jsonText);
        url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+wayLatitude+","+wayLongitude+"&minprice&type=mall&rankby=distance&key=...";
        stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
                new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        myQueue.getCache().invalidate(url, true);
                        myQueue.getCache().clear();
                        // print response...
                        // old response is beeing printed!!
                    }
                }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                textView.setText("Network Error, please close the app and try again");
            }
        });
        myQueue.add(stringRequest);

    }

应打印旧回复...

0 个答案:

没有答案