排球获取请求根本不执行

时间:2018-08-17 21:02:12

标签: android get request android-volley

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");

运行此代码时,将打印Check 1和Check 2(日志),但是Volley请求既未给出错误也未给出响应(因此,其他Log消息均未打印)。我尝试将网址更改为无效的内容,然后引发404错误,但我确定该网址正确。我已经看了好几个小时,当我在另一个片段中运行它时,它运行得很好。但是它不会在这里运行,也不会在我将其放在另一个按钮的onClickListener中的时候运行。

1 个答案:

答案 0 :(得分:3)

所以发现了我自己的错误:

创建一个Google地图是一个异步功能,后端中的代码也是如此(节点js)。为了解决这个问题,我将所有代码放入onMapCreate函数中。

//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);