[android]不执行截击 JSON onResponse 函数

发布时间: 2016/9/28 1:51:56
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

当我调用 getData.It 似乎很难得到从 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);
}

解决方法 1:

试试这个我认为它应该工作

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);
}
官方微信
官方QQ群
31647020