Java ArrayList函数在Android中返回null
我试图从远程服务器返回值,将其添加到Java ArrayList函数在Android中返回null,java,android,android-studio,arraylist,android-volley,Java,Android,Android Studio,Arraylist,Android Volley,我试图从远程服务器返回值,将其添加到ArrayList中,并将其显示在Android studio中我的抽屉中,但我得到了null。volley中的名字和姓氏包含来自我的远程服务器的名字,但是名称ArrayList返回null GetName.java public List<String> getDriverName(Context contenxt){ String COMPLETE_URL = TRANSACTION_DETAIL_URL + Preference
ArrayList
中,并将其显示在Android studio中我的抽屉中,但我得到了null
。volley中的名字和姓氏包含来自我的远程服务器的名字,但是名称ArrayList
返回null
GetName.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;
}
结果
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);
}
public void getDriverName(上下文上下文){
String COMPLETE\u URL=事务\u DETAIL\u URL+首选项.getDefaults(“email”,contenxt);
Log.i(“url”,完整的url);
name=新的ArrayList();
StringRequest StringRequest=新建StringRequest(Request.Method.GET,COMPLETE_URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.i(“info”,response.toString());
JSONObject JSONObject=null;
试一试{
jsonObject=新的jsonObject(响应);
JSONArray JSONArray=jsonObject.getJSONArray(“成功”);
if(jsonArray.length()>0){
for(int i=0;i Empty和null不是一回事。考虑到打印日志消息的顺序,您似乎存在异步问题,您的方法在数据到达之前返回
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);
}