簡體   English   中英

ArrayList 函數在 Android 中返回 null

[英]ArrayList function returns null in Android

我試圖從我的遠程服務器返回值,將它添加到一個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

您在數據尚未到達之前返回name變量。

您應該在數據到來並分配給該變量后處理name變量。

你可以這樣做:

NameAndEmail nameAndEmail = new NameAndEmail();

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

您的getDriverName()方法將類似於:

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);
}

希望它會起作用。 謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM