android - 不执行 Volley JSON onResponse 函数

标签 android json android-studio

当我调用 getData 时,似乎很难从 onResponse 中获取结果。我知道它不能以目前的方式工作。谁能帮我解决这个问题?

getData()

private void getData(){

    //Creating a string request
    StringRequest stringRequest = new StringRequest(SPINNER_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
           // Log.d("Country_name","hi");
            JSONObject j = null;
            try {
                //Parsing the fetched Json String to JSON Object
                j = new JSONObject(response);

                //Storing the Array of JSON String to our JSON Array
                result = j.getJSONArray(JSON_ARRAY);

                Log.v("xxxxx",result.toString());
                String mysh=result.toString().substring(1, result.toString().length()-1);


                JSONArray jsonArray = new JSONArray(mysh);
                //Calling method getCountry to get the country from the JSON Array
                getCountry(jsonArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

最佳答案

尝试一下,我认为它应该有效

private void getData(){

//Creating a string request
StringRequest stringRequest = new StringRequest(SPINNER_URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {


        try {

            JSONArray jsonArray = new JSONArray(response);
            getCountry(jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
},
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


RequestQueue requestQueue = Volley.newRequestQueue(this);


requestQueue.add(stringRequest);
}

关于android - 不执行 Volley JSON onResponse 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545929/

相关文章:

android - 使用默认图像而不是默认图像资源 ID 的 Volley NetworkImageView

javascript - 解析 JSON 的问题——作为 application/x-www-form-urlencoded 发送的 ajax 请求

android-studio - 除了枚举注释外,kotlin Android还可以替代枚举吗?

java - Content-Disposition 值 : form-data; name ="userfile"; filename ="IMG_٢٠١٩١١١٥_٠٩٠٢٠٤١١٤.jpg" 中 42 处的意外字符 0x662

android - Oreo 的依赖项更改会出错

android - 可扩展布局与 recyclerview android

android - MapFragment - NullPointerException - 谷歌地图 V2 - Android

arrays - 传递给不带参数的调用的参数 Data(contentOf :)

jquery - 帮助重构 ajax 调用中的 jquery 片段

android - 模拟器 : Process finished with exit code -1073741515 (0xC0000135)