当前位置:  开发笔记 > 编程语言 > 正文

Android Volley - 没有来自StringRequest的回复

如何解决《AndroidVolley-没有来自StringRequest的回复》经验,为你挑选了1个好方法。

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() {
                    @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());
    }

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



1> Niels Masdor..:

你在哪里实际执行请求?你需要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() {
            @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);

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

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有