I am trying to return value from my remote server, add it to an ArrayList
and display it on my drawer in Android studio but I am getting null
. The first name and last name in volley contains that from my remote server, but the name ArrayList
returns 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;
}
Dashboard Activity
NameAndEmail nameAndEmail = new NameAndEmail();
Log.d(null, "result: " + nameAndEmail.getDriverName(getApplicationContext()));
Result
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.