Volley Get 请求根本没有执行
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");
当我 运行 此代码时,检查 1 和检查 2(日志)打印,但 Volley 请求既不给出错误也不给出响应(所以其他日志消息的 none 打印).我尝试将 url 更改为无效的内容,然后它引发了 404 错误,但我确信 url 是正确的。我已经看了几个小时了,当我在另一个片段中 运行 时,它 运行 非常完美。但它不会 运行 在这里,或者当我把它放在另一个按钮的 onClickListener
中时。
所以发现了我自己的错误:
创建 google 地图是一个异步函数,就像我后端(node 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);
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");
当我 运行 此代码时,检查 1 和检查 2(日志)打印,但 Volley 请求既不给出错误也不给出响应(所以其他日志消息的 none 打印).我尝试将 url 更改为无效的内容,然后它引发了 404 错误,但我确信 url 是正确的。我已经看了几个小时了,当我在另一个片段中 运行 时,它 运行 非常完美。但它不会 运行 在这里,或者当我把它放在另一个按钮的 onClickListener
中时。
所以发现了我自己的错误:
创建 google 地图是一个异步函数,就像我后端(node 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);