android – Volley获取请求根本不执行

前端之家收集整理的这篇文章主要介绍了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

当我运行此代码时,检查1和检查2(日志)打印,但是Volley请求既没有错误也没有响应(因此没有其他日志消息打印).我尝试将url更改为无效的内容,然后它抛出了404错误,但我确定该url是正确的.我已经看了几个小时了,当我用另一个片段运行时,它运行得很好.但它不会在这里运行,或者当我把它放在另一个按钮的onClickListener中时.

最佳答案
所以发现了我自己的错误

创建谷歌地图是异步功能,我的后端(节点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,new Response.Listener

猜你在找的Android相关文章