Android post authorization -
i trying http post server check if login credentials valid of this tutorial. need make request server have add authorization, string function getb64auth this answer. function logs right variable, (the same use postman). reason program stops running if run code. tried adding code comments didn't help.
what doing wrong?
private string getb64auth (string login, string pass) { string source=login+":"+pass; string ret="basic "+base64.encodetostring(source.getbytes(),base64.url_safe| base64.no_wrap); log.d("authy", ret); return ret; } /** called when user clicks login button */ public void login(view view) { // getting username , password edittext usertext = (edittext) findviewbyid(r.id.inputloginusername); edittext passtext = (edittext) findviewbyid(r.id.inputloginpassword); string usernameinput = usertext.gettext().tostring(); string passwordinput = passtext.gettext().tostring(); string authorizationstring = getb64auth(usernameinput,passwordinput ); // in response button // 1. create object of httpclient httpclient httpclient = new defaulthttpclient(); // 2. create object of httppost // have real server name down here httppost httppost = new httppost("https://myservername.com/login"); // 3. add post parameters httppost.setheader("authorization", authorizationstring); // 5. making http post request try { httpresponse response = httpclient.execute(httppost); log.d("win", "win1"); // write response log log.d("http post response:", response.tostring()); } catch (clientprotocolexception e) { log.d("fail", "fail 3"); // log exception e.printstacktrace(); } catch (ioexception e) { log.d("fail", "fail 4"); // log exception e.printstacktrace(); } }
when run code app stops working, can find log authy cant find fail succes logs. things have changed in example step 3.
ive added authorization there.
and removed step 4 cause dont need it.
working postman example, same request want make in android.
you can see response, , set authorization on request.
i cant find decent post/authorization tutorials hope i'm looking @ right direction.
it's android 4.* project
just few suggestions such issues:
- check permissions (internet 1 need)
- applications charles / fiddler let sniff http traffic device investigate being sent
- if application crashing - check logcat messages (for example contain message explaining permission missing)
regarding message:
the application may doing work on main thread.
this means doing heavy operations in main thread - example parsing json http response. you'd these operations in background thread , use main 1 update ui only.