java - OkHttp Request in Separate Class -
so have okhttp requests in mainactivity in order test worked. however, need move them separate class can use requests populate navagation drawer menu items. furthermore, in mainactivity, oauth of invalidating token , requesting , token needs used in requests. bit lost calling new class in mainactivity apirequest.run();
in run console.
i/system.out: ya29.cjbiax67rm70-cku9wc5fuslzt9riqt4brubpl4hb9ujwqeratozppbmmndl_spcfwa e/authapp: ya29.cjbiax67rm70-cku9wc5fuslzt9riqt4brubpl4hb9ujwqeratozppbmmndl_spcfwa w/system.err: java.lang.nullpointerexception: attempt invoke interface method 'java.lang.string android.content.sharedpreferences.getstring(java.lang.string, java.lang.string)' on null object reference w/system.err: @ com.example.jamessingleton.chffrapi.authpreferences.gettoken(authpreferences.java:43) w/system.err: @ com.example.jamessingleton.chffrapi.apirequests.run(apirequests.java:34) w/system.err: @ com.example.jamessingleton.chffrapi.mainactivity$1.onclick(mainactivity.java:90) w/system.err: @ com.google.android.gms.common.signinbutton.onclick(unknown source) w/system.err: @ android.view.view.performclick(view.java:5697) w/system.err: @ android.widget.textview.performclick(textview.java:10814) w/system.err: @ android.view.view$performclick.run(view.java:22526) w/system.err: @ android.os.handler.handlecallback(handler.java:739) w/system.err: @ android.os.handler.dispatchmessage(handler.java:95) w/system.err: @ android.os.looper.loop(looper.java:158) w/system.err: @ android.app.activitythread.main(activitythread.java:7224) w/system.err: @ java.lang.reflect.method.invoke(native method) w/system.err: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) w/system.err: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)
here mainactivity implementing it.
context mcontext = mainactivity.this; private accountmanager maccountmanager; private authpreferences authpreferences; private apirequests apirequests; edittext emailtext; textview responseview; progressbar progressbar; static final string api_url = "https://api.comma.ai/v1/auth/?access_token="; static final string chffrme_url = "https://api.comma.ai/v1/me/"; static final string scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me"; private static final int authorization_code = 1993; private static final int account_code = 1601; string commatoken; string commamyinfo; private final okhttpclient client = new okhttpclient(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); responseview = (textview) findviewbyid(r.id.responseview); progressbar = (progressbar) findviewbyid(r.id.progressbar); maccountmanager = accountmanager.get(this); authpreferences = new authpreferences(this); apirequests = new apirequests(); final context context = this; invalidatetoken(); requesttoken(); signinbutton signinbutton = (signinbutton) findviewbyid(r.id.sign_in_button); signinbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (authpreferences.getuser() != null && authpreferences.gettoken() != null) { system.out.println(authpreferences.gettoken()); docoolauthenticatedstuff(); // intent intent = new intent(context, navdraweractivity.class); // startactivity(intent); try { apirequests.run(); } catch (exception e) { e.printstacktrace(); } // try { // run(); // } catch (exception e) { // e.printstacktrace(); // } //new retrievefeedtask().execute(); } else { chooseaccount(); } } });
and here apirequests.java
public class apirequests { private final okhttpclient client = new okhttpclient(); static final string api_url = "https://api.comma.ai/v1/auth/?access_token="; static final string chffrme_url = "https://api.comma.ai/v1/me/"; private authpreferences authpreferences = new authpreferences(this); string commatoken; string commamyinfo; private mainactivity mactivity; public void run() throws exception { request request = new request.builder() .url(api_url + authpreferences.gettoken()) .build(); client.newcall(request).enqueue(new callback() { public void onfailure(call call, ioexception e) { e.printstacktrace(); } public void onresponse(call call, response response) throws ioexception { if (!response.issuccessful()) throw new ioexception("unexpected code " + response); headers responseheaders = response.headers(); (int = 0, size = responseheaders.size(); < size; i++) { system.out.println(responseheaders.name(i) + ": " + responseheaders.value(i)); } try { string token = response.body().string(); jsonobject json = new jsonobject(token); commatoken = json.getstring("access_token"); } catch (jsonexception e) { } // commatoken = response.body().string(); // system.out.println(commatoken); if(response.issuccessful()) { final request datarequest = new request.builder() .header("content-type", "application/x-www-form-urlencoded") .header("authorization", "jwt "+ commatoken) .url(chffrme_url).build(); client.newcall(datarequest).enqueue(new callback() { @override public void onfailure(call call, ioexception e) { e.printstacktrace(); } @override public void onresponse(call call, response responseme) throws ioexception { if (!responseme.issuccessful()) throw new ioexception("unexpected code " + responseme); headers responseheaders = responseme.headers(); (int = 0, size = responseheaders.size(); < size; i++) { system.out.println(responseheaders.name(i) + ": " + responseheaders.value(i)); } try { string myinfo = responseme.body().string(); jsonobject json = new jsonobject(myinfo); commamyinfo = json.getstring("points"); system.out.println(commamyinfo); } catch (jsonexception e) { } // runonuithread(new runnable() { // @override // public void run() { // responseview.settext("comma points: " +commamyinfo); // } // }); } }); } } }); } }
not sure why isn't liking apirequests.run();
here authpreferences
public class authpreferences { private static final string key_user = "user"; private static final string key_token = "token"; private sharedpreferences preferences; public authpreferences(context context) { preferences = context .getsharedpreferences("auth", context.mode_private); } public authpreferences(apirequests apirequests) { } public void setuser(string user) { editor editor = preferences.edit(); editor.putstring(key_user, user); editor.commit(); } public void settoken(string password) { editor editor = preferences.edit(); editor.putstring(key_token, password); editor.commit(); } public string getuser() { return preferences.getstring(key_user, null); } public string gettoken() { return preferences.getstring(key_token, null); } }
your authpreferences
class has 2 constructors. 1 sets preferences
field. other not. crash when use constructor not set preferences
field, field null
when try using it.
i recommend getting rid of second constructor. then, add constructor apirequests
takes authpreferences
parameter, , use populate authpreferences
field (rather creating new, flawed instance of authpreferences
). then, in activity, replace apirequests = new apirequests();
apirequests = new apirequests(authpreferences);
, meet requirements of new constructor.
Comments
Post a Comment