android - VKRequest after VKSdk.authorize() throws error "invalid user id". One after another? -


i make request user profile info clicking button. debugged , when opened app , clicked on button onerror callback worked , said "invalid user id". if press button second time works fine. experiments understood app didn't complete authorization, that's why request denied. pressing button calls method code:

vksdk.authorize(vkscopes.email); vkrequest request = vkapi.users().get(vkparameters.from(vkapiconst.fields,     "user_id,first_name,last_name,sex,bdate,city,photo_200_orig")); request.secure = false; request.usesystemlanguage = true; request.executewithlistener(new vkrequest.vkrequestlistener() {      @override      public void oncomplete(vkresponse response) {          super.oncomplete(response);          //do stuff      }      @override      public void attemptfailed(vkrequest request, int attemptnumber, int totalattempts) {           super.attemptfailed(request, attemptnumber, totalattempts);           log.d("vkdemoapp", "attemptfailed " + request + " " + attemptnumber + " " + totalattempts);     }     @override     public void onerror(vkerror error) {          super.onerror(error);          log.d("vkdemoapp", "onerror: " + error);     }     @override     public void onprogress(vkrequest.vkprogresstype progresstype, long bytesloaded, long bytestotal) {          super.onprogress(progresstype, bytesloaded, bytestotal);          log.d("vkdemoapp", "onprogress " + progresstype + " " + bytesloaded + " " + bytestotal);     } }); 

i tried add if(vksdk.wakeupsession()) , passed through, cuz user not authorized yet. found in example callback ontokenexpired part of listener, i'll authorize user if needed, when added said "is never used".

how can wait until vksdk.authorize() finish working , after send request? in advance!

edit

this line in oncreate()

vksdk.initialize(sdklistener, activity.getresources().getstring(r.string.vk_app_id)); vkuihelper.oncreate(this); 

vksdklistener

vksdklistener sdklistener = new vksdklistener() {     @override     public void onacceptusertoken(vkaccesstoken token) {     }      @override     public void onreceivenewtoken(vkaccesstoken newtoken) {         //requesting code @ top (request)         //excluding line vksdk.authorize(vkscopes.email);     }      @override     public void onrenewaccesstoken(vkaccesstoken token) {     }     @override     public void oncaptchaerror(vkerror vkerror) {     }     @override     public void ontokenexpired(vkaccesstoken vkaccesstoken) {     }     @override     public void onaccessdenied(vkerror vkerror) {     } }; 

and in onclick() there line vksdk.authorize(vkscopes.email);

note vksdklistener's callbacks not being called after initialize(), called after authorize(). don't know why, behavior of awful. great see working example gotten user's email , other things listed above.

i wrong, try change "user_id" "id". answer second question (how fetch data after authorize) simple. vksdk has listener abstract methods (and should) override.

edit

private static final string[] svkontaktescopes = new string[]{         vkscope.friends,         vkscope.photos,         vkscope.nohttps, };  private vksdklistener mvkontakteloginlistener;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate, persistentstate);      mvkontakteloginlistener = new vksdklistener() {         @override         public void oncaptchaerror(vkerror captchaerror) {             new vkcaptchadialog(captchaerror).show(vkapiactivity.this);         }          @override         public void ontokenexpired(vkaccesstoken expiredtoken) {             vksdk.authorize(svkontaktescopes);         }          @override         public void onaccessdenied(vkerror authorizationerror) {             new alertdialog.builder(vkuihelper.gettopactivity())                     .setmessage(authorizationerror.tostring())                     .show();         }          @override         public void onreceivenewtoken(vkaccesstoken newtoken) {             //put method here             makeyourstuff();             prefutils.storevkontakteaccesstoken(newtoken.accesstoken);         }          @override         public void onacceptusertoken(vkaccesstoken token) {             //put method here             makeyourstuff();         }          @override         public void onrenewaccesstoken(vkaccesstoken token) {             //put method here             makeyourstuff();         }     };      if (prefutils.getvkontakteaccesstoken() != null)         vksdk.initialize(mvkontakteloginlistener, getstring(r.string.vkontakte_app_id, prefutils.getvkontakteaccesstoken()));     else {         vksdk.initialize(mvkontakteloginlistener, getstring(r.string.vkontakte_app_id));     }     vkuihelper.oncreate(this); }   private void makeyourstuff() {     vkparameters params = new vkparameters();     params.put(vkapiconst.fields, "id,first_name,last_name, photo_100");      vkrequest request = new vkrequest("users.get", params);     request.executewithlistener(new vkrequest.vkrequestlistener() {          @override         public void oncomplete(vkresponse response) {             super.oncomplete(response);             jsonarray jsonarray;             string firstname, lastname, id, photourl;               try {                 jsonarray = response.json.getjsonarray("response");                 jsonobject userobject = jsonarray.getjsonobject(0);                 firstname = userobject.getstring("first_name");                 lastname = userobject.getstring("last_name");                 id = userobject.getstring("id");                 photourl = userobject.getstring("photo_100");                  /*                     here can start activity or fragment                      put info bundle or store database                  */             } catch (jsonexception e) {                 e.printstacktrace();             }         }          @override         public void onerror(vkerror error) {             super.onerror(error);         }     }); }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);     vkuihelper.onactivityresult(this, requestcode, resultcode, data); }  @override public void onclick(view v) {     if (v.getid() == r.id.btnlogin) {         vksdk.authorize(svkontaktescopes);         // after method called 1 of          vksdklistener methods         // should put request method 3 success methods of vksdk listener     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -