S
S
Sharombolla2015-10-06 17:41:32
Java
Sharombolla, 2015-10-06 17:41:32

How to save the data received using the VK SDK into a separate object?

Hello! I am a beginner Android developer, I really hope for your help!
I am developing an application that is logged in through social networks. network "Vkontakte". It was possible to implement login and registration, now we need to store information about the user in a separate object, and here a problem arose: when I try to save data to an external object from the overloaded public void onComplete(VKResponse response) method of the VKRequestListener object at the end of the operation, the external object simply does not saves (I suspect that it is destroyed along with the VKRequestListener object ), although at runtime you can track that the data is assigned and the fields of the external object are changed.
Here is the class code that is responsible for calling the VK Api:

import com.vk.sdk.api.VKApi;
import com.vk.sdk.api.VKApiConst;
import com.vk.sdk.api.VKError;
import com.vk.sdk.api.VKParameters;
import com.vk.sdk.api.VKRequest;
import com.vk.sdk.api.VKRequest.VKRequestListener;
import com.vk.sdk.api.VKResponse;
import com.vk.sdk.api.model.VKApiUserFull;
import com.vk.sdk.api.model.VKList;
import com.prosto.asus.goroda.Player;

public class ApiVkCall {

    private VKRequest myRequest;
    private Player playerInfo;

    VKRequestListener mRequestListener = new VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            if (((VKList<VKApiUserFull>) response.parsedModel).size() == 1) {
                VKApiUserFull user = ((VKList<VKApiUserFull>) response.parsedModel).get(0);
                getUserInfo(user.first_name, user.last_name, user.photo_200);
            } else {
                getUserFriends((VKList<VKApiUserFull>) response.parsedModel);
            }
        }
    };

    public ApiVkCall() {
        this.myRequest = null;
        this.playerInfo = new Player (null, null, null, 0, 0, 0, 0, 0);
    };

    public void setRequest(String textReq) {
        this.myRequest = VKApi.users().get(VKParameters.from(VKApiConst.FIELDS, textReq));
        myRequest.secure = false;
        myRequest.useSystemLanguage = false;
    }

    public void processRequestIfRequired() {
        VKRequest request = null;

        if (this.myRequest != null) {
            long requestId = this.myRequest.registerObject();
            request = VKRequest.getRegisteredRequest(requestId);
            if (request != null)
                request.unregisterObject();
        }

        if (request == null) return;
        myRequest = request;
        request.executeWithListener(mRequestListener);
    }

    protected void getUserInfo(String first_name, String last_name, String photo_200) {

        playerInfo.setName(first_name);
        playerInfo.setLastName(last_name);
        playerInfo.setAvatarUrl(photo_200);
    }

    protected void getUserFriends (VKList<VKApiUserFull> friendList) {
        playerInfo.setFriendList(friendList);
    }
}

Here is a part of the activity code that, if a valid AccessToken is received, should save the basic data about the user:
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        VKCallback<VKAccessToken> callback = new VKCallback<VKAccessToken>() {
            @Override
            public void onResult(VKAccessToken res) {
                // User passed Authorization
                //startTestActivity();
                ApiVkCall newCall = new ApiVkCall();
                //получить информацию об игроке
                newCall.setRequest("photo_200");
                newCall.processRequestIfRequired();
                //получить список друзей игрока в ВК
                newCall.setRequest("id,first_name,last_name,photo_200");
                newCall.processRequestIfRequired();
            }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question