Search code examples
androidgetrequestandroid-volley

Volley Get Request not executing at all


public class RouteFragment extends Fragment implements LocationListener {

MapView mMapView;
private GoogleMap googleMap;
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
private String startLocation, endLocation = "";
private LatLng start, end;
private CarouselView carouselView;
private String estimatedDistance = "0km";
private TextView costPerMile;
public int counter = 0;
public static final String[] vehicleList = {"ambulance", "wheelchair", "star"};
private int chosenVehicle = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_route, container, false);
    final Context context = getActivity().getApplicationContext();
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        startLocation = bundle.getString("start_location");
        endLocation = bundle.getString("end_location");
    }

    RequestQueue queue2 = Volley.newRequestQueue(context);
    StringRequest getRequest2 = new StringRequest(Request.Method.GET, (MainActivity.url + "/api/getLocation" + "?place=" + startLocation),
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jresponse = new JSONObject(response);
                        Log.d("END", response);
                        Double lat = Double.valueOf(jresponse.getJSONObject("data").getString("lat"));
                        Double lng = Double.valueOf(jresponse.getJSONObject("data").getString("lng"));
                        start = new LatLng(lat,lng);

                    } catch (JSONException e) {
                        Log.d("ASD", "ERROR");
                        Log.d("ERROR", String.valueOf(e));
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", String.valueOf(error));
                }
            }
    ) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError
        {
            Map<String, String> headers = new HashMap<String, String>();
            //Log.d("ASD", MainActivity.token);
            headers.put("x-access-token", MainActivity.token);
            headers.put("Content-Type","application/x-www-form-urlencoded");
            return headers;
        }
    };

    queue2.add(getRequest2);

    checkLocationPermission();
    FloatingActionButton fab = getActivity().findViewById(R.id.fab);
    fab.setVisibility(View.INVISIBLE);
    mMapView = view.findViewById(R.id.mapRoute);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume(); // needed to get the map to display immediately
    Log.d("ASD", "ASDASDASDASDASD");

When I run this code, Check 1 and Check 2 (the logs) print, but the Volley request gives neither an error or a response (so none of the other Log messages print). I tried changing the url to something that was invalid, and then it threw a 404 error, but I am certain that the url is right. I've been looking at it for hours, and when I ran it in another fragment, it ran perfectly. But it wont run here, or when I put it inside a onClickListener of another button either.


Solution

  • so found my own mistake:

    creating a google map is an async function, as is the code in my backend (node js). To fix this, I put all my code inside the onMapCreate function.

    //This goes in the onCreate
    SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);
    
    //This overrides the onMapReady function
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
        StringRequest getRequest = new StringRequest(Request.Method.GET, (MainActivity.url + "/api/getLocation" + "?place=" + startLocation),
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jresponse = new JSONObject(response);
                        }
                        catch (JSONException e) {
                                        Log.d("ASD", "ERROR");
                                        Log.d("ERROR", String.valueOf(e));
                                        e.printStackTrace();
                        }
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error
                        Log.d("Error.Response", String.valueOf(error));
                        }
                }
        )
    queue.add(getRequest);