java - 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
解决方案
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.
推荐阅读
- python - 无法更改 tkinter 选项卡式显示的背景
- r - 如何按特定的兴趣时间对数据进行子集化?
- python - 尝试在标签中显示图像时 setPixmap 崩溃
- react-native - 使用 create-react-native-app 创建新项目时出现语法错误
- javascript - 是否可以将请求标头添加到 RxJs Websocket?
- java - 根据Java中的另一个列表获取具有最高优先级的元素
- laravel - laravel 可以针对重复的 uri 参数进行验证吗?
- python - Python AtributeError: __enter__ 在 `with as` 语句中使用 urlopen
- javascript - 检测文档上的第一次用户交互
- scheme - Racket中list和list*的区别