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

    1 answer 1

     search_users.executeWithListener(new VKRequest.VKRequestListener() { 

    It VKRequest.VKRequestListener() asynchronously and calls VKRequest.VKRequestListener() when a response is received.

    Accordingly, here you have rez still empty, because the listener has not yet worked.

     Log.e("VK_FUNCTION_REZULT", String.valueOf(rez)); return rez; //возвращает "" 

    UPD

    In fact, it is a very bad idea to synchronously expect a response from a method whose result depends on the nested asynchronous code.

    There are two ways, one is bad, the other is good.

    1) Bad. Very bad. Do not even think about using it, just cite to understand how it all works.

      catch (JSONException je){ je.printStackTrace(); } } }); while(rez.size() == 0){ //курим } return rez; 

    2) Usually done differently.

    We change the signature of your method as follows.

     public static void getUsers(String city, int ofset) 

    Add another method that accepts the result and does what you want.

     public static void doSomethingWhenUsersReceived(ArrayList<String> rez){ //обрабатываем результат } 

    In the body of the listener, when the result is obtained, we call this method.

      MyClass.doSomethingWhenUsersReceived(rez); } catch (JSONException je) { 
    • I get an error Error:(403, 24) error: incompatible types: unexpected return value - vladimir
    • one
      @vladimir I deceived you a little, not out of malice. I’ll correct the answer now - rjhdby
    • @vladimir completed the answer - rjhdby
    • well, and now how to get for example a TextView in a function that should process the received data? PS I apologize for such simple questions, I just didn’t come across such a thing before and I can’t find anything in google either = ( - vladimir
    • everything, understood =) @rjhdby many thanks for the help !!!! - vladimir