首页 > 解决方案 > ArrayList 函数在 Android 中返回 null

问题描述

我正在尝试从远程服务器返回值,将其添加到ArrayList并显示在 Android Studio 的抽屉中,但我得到null. volley 中的名字和姓氏包含来自我的远程服务器的名字,但名称ArrayList返回null

获取名称.java

  public List<String> getDriverName(Context contenxt){
    String COMPLETE_URL = TRANSACTION_DETAIL_URL +  Preferences.getDefaults("email", contenxt);
    Log.i("url", COMPLETE_URL);

    name = new ArrayList<String>();

    StringRequest stringRequest = new StringRequest(Request.Method.GET, COMPLETE_URL, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {
            Log.i("info", response.toString());

            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("success");
                if (jsonArray.length() > 0){
                    for (int i=0; i <= jsonArray.length()-1; i++){
                        JSONObject productJson = jsonArray.getJSONObject(i);
                        first_name =productJson.getString("firstName");
                        last_name = productJson.getString("lastName");
                        name.add(first_name);
                        name.add(last_name);
                        Log.d(null, "onResponse: " + last_name);

                    }

                }else{
                    first_name = "";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            first_name = "";            }
    });
    int socketTimeout = 10000;
    RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    RequestQueue requestQueue = Volley.newRequestQueue(contenxt);
    requestQueue.add(stringRequest);

    return name;
}

仪表板活动

NameAndEmail nameAndEmail = new NameAndEmail();
Log.d(null, "result: " + nameAndEmail.getDriverName(getApplicationContext()));

结果

D/: result: []

W/art: Before Android 4.1, method int     
android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, 
boolean) would have incorrectly overridden the package-private method in 
android.widget.ListView
I/info: {"success":[{"firstName":"usfdh","lastName":"hseheh"}]}
D/: onResponse: hseheh

标签: javaandroidandroid-studioarraylistandroid-volley

解决方案


You are returning name variable before your data has not arrived.

You should process on name variable after data came and assigned to that variable.

You can do something like this:

NameAndEmail nameAndEmail = new NameAndEmail();

getDriverName(getApplicationContext()); // Remove return params from method.

Your getDriverName() method will be like:

public void getDriverName(Context contenxt){
    String COMPLETE_URL = TRANSACTION_DETAIL_URL +  Preferences.getDefaults("email", contenxt);
    Log.i("url", COMPLETE_URL);

    name = new ArrayList<String>();

    StringRequest stringRequest = new StringRequest(Request.Method.GET, COMPLETE_URL, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {
            Log.i("info", response.toString());

            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("success");
                if (jsonArray.length() > 0){
                    for (int i=0; i <= jsonArray.length()-1; i++){
                        JSONObject productJson = jsonArray.getJSONObject(i);
                        first_name =productJson.getString("firstName");
                        last_name = productJson.getString("lastName");
                        name.add(first_name);
                        name.add(last_name);
                        Log.d(null, "onResponse: " + last_name);

                        /******************************
                        ****** DO PROCESS HERE ********
                        ******************************/

                    }

                }else{
                    first_name = "";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            first_name = "";            }
    });
    int socketTimeout = 10000;
    RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    RequestQueue requestQueue = Volley.newRequestQueue(contenxt);
    requestQueue.add(stringRequest);
}

Hope it will work. Thank you.


推荐阅读