Android Volley - 没有来自StringRequest的回复

anu*_*der 0 connection android json android-volley

JSON使用Android Volley时我无法获得响应.没有错误也没有Succesfull响应see(Log.d("logr=",_response);).我使用StringRequest从Google Map API Android V2获取JSON文本.这是代码

private String urla = "https://maps.googleapis.com/maps/api/place/search/json?location=";
private String urlb = "&types=hotel&radius=500&sensor=false&key=YOUR_KEY";


   @Override //::LocationListener (Interface)
    public void onLocationChanged(Location location) {
        //Log.d(TAG, "Firing onLocationChanged..............................................");
        mCurrentLocation = location;
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()), 13);
        googleMap.animateCamera(cameraUpdate);

        double x = location.getLatitude();
        double y = location.getLongitude();

        String request = urla+x+","+y+urlb;
        Log.d("log1=",request);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, request,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String _response) {

                        Log.d("logr=",_response);

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                // Error handling
                Log.d("log2=", error.toString());
                Log.e("log3=", error.toString());
            }
        });


        Toast.makeText(activity, "Update location user ==>" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Current Location :" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude());
    }
Run Code Online (Sandbox Code Playgroud)

解决这个问题的任何方法?

Nie*_*orp 7

你在哪里实际执行请求?你需要add()它到Volley的RequestQueue,否则请求根本不会发送.

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);

String url = urla + x + "," + y + urlb;

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String _response) {

                Log.d("logr=",_response);

            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

        // Error handling
        Log.d("log2=", error.toString());
        Log.e("log3=", error.toString());
    }
});
//excecute your request
queue.add(stringRequest);
Run Code Online (Sandbox Code Playgroud)

在这里查看更多:学习排球