Hello. There was a question about how to return the data received from VK API Android SDK? I have a function that should return the received data that VKResponse returned however when I do return data , the function returns an empty value ...
function code
public static ArrayList<String> getUsers(String city, int ofset) { final ArrayList<String> rez = new ArrayList<>(); VKRequest search_users = new VKRequest("users.search", VKParameters.from("count", 1, "hometown", city, "sex", 1, "status", 6, "offset", ofset, VKApiConst.FIELDS, "photo_max_orig, contacts, last_seen, photo_id")); search_users.executeWithListener(new VKRequest.VKRequestListener() { @SuppressLint("LongLogTag") @Override public void onComplete(VKResponse response) { super.onComplete(response); try { jUsers = new JSONObject(response.responseString); userObject = jUsers.getJSONObject("response"); usersArray = userObject.getJSONArray("items"); Log.e("VK_FOUND_USERS_ARRAY", String.valueOf(usersArray)); for (int i = 0; i < usersArray.length(); i++) { object = usersArray.getJSONObject(i); Log.e("VK_CONCRETE_USER_OBJECT", String.valueOf(object)); rez.add( object.getString("first_name") ); rez.add( object.getString("last_name") ); rez.add( object.getString("photo_max_orig") ); if ( object.has("home_phone") ) rez.add( object.getString("home_phone") ); if ( object.has("mobile_phone") ) rez.add( object.getString("mobile_phone") ); } } catch (JSONException je) { je.printStackTrace(); } } }); Log.e("VK_FUNCTION_REZULT", String.valueOf(rez)); return rez; //возвращает "" } ZY in Java and Android, I'm still a newbie, so do not judge strictly by the code and such a question =)
UPDATE 1 here is the function code as suggested by @rjhdby
public static ArrayList<String> getUsers(String city, int ofset) { final ArrayList<String> rez = new ArrayList<>(); VKRequest search_users = new VKRequest("users.search", VKParameters.from("count", 1, "hometown", city, "sex", 1, "status", 6, "offset", ofset, VKApiConst.FIELDS, "photo_max_orig, contacts, last_seen, photo_id")); search_users.executeWithListener(new VKRequest.VKRequestListener() { @SuppressLint("LongLogTag") @Override public void onComplete(VKResponse response) { super.onComplete(response); try { jUsers = new JSONObject(response.responseString); userObject = jUsers.getJSONObject("response"); usersArray = userObject.getJSONArray("items"); Log.e("VK_FOUND_USERS_ARRAY", String.valueOf(usersArray)); for (int i = 0; i < usersArray.length(); i++) { object = usersArray.getJSONObject(i); Log.e("VK_CONCRETE_USER_OBJECT", String.valueOf(object)); rez.add( object.getString("first_name") ); rez.add( object.getString("last_name") ); rez.add( object.getString("photo_max_orig") ); if ( object.has("home_phone") ) rez.add( object.getString("home_phone") ); if ( object.has("mobile_phone") ) rez.add( object.getString("mobile_phone") ); } } catch (JSONException je) { je.printStackTrace(); } Log.e("VK_FUNCTION_REZULT", String.valueOf(rez)); return rez; //Error:(403, 24) error: incompatible types: unexpected return value } }); }