从外部函数返回数组并获得结果

时间:2016-02-19 22:56:05

标签: java android android-fragments

我遇到了Java问题。 我有这个功能" fillArray()"我需要这个函数返回一个数组。数组值由JSON获取。

此功能的代码是下一个:

  public String[] fillArray(){

    requestQueue = Volley.newRequestQueue(getActivity());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://api.mysite.com/v1/countries",
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("data");

                        final String[] countries = new String[jsonArray.length()];

                        for (int i = 0; i < jsonArray.length(); i++){
                            JSONObject country = jsonArray.getJSONObject(i);

                            String country_name = country.getString("country_name");
                            countries[i] = country_name;
                            Log.v("NAME:", country_name);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.e("VOLLEY", "ERROR");
                }
            }
    );

    requestQueue.add(jsonObjectRequest);
    return countries;
}

我可以返回数组,我收到错误&#34;无法解析符号国家&#34;。

有什么想法吗?提前谢谢!

0 个答案:

没有答案
相关问题