android - Add Dynamic Header and Body in retrofit request -
i using retrofit 2
, secure api using token key need add token request.
here endpoint:
http://192.168.1.22:8000/api/auth/queue/store
post request retrofit:
@multipart @post("auth/queue/store") call<responsebody> registerqueue(@header("authorization") string authorization,@part("task") requestbody task);
activity:
public void sendnetworkrequest(string token, string task) { string temp ="bearer "+token; requestbody taskbody = requestbody.create(mediatype.parse("text/plain"), task); log.d(tag, "send network request: task: "+task+" "+" "+ temp ); retrofit.builder builder = new retrofit.builder() .baseurl("http://192.168.1.22:8000/api/") .addconverterfactory(gsonconverterfactory.create()); retrofit retrofit = builder.build(); apiinterface apiinterface = retrofit.create(apiinterface.class); call<responsebody> call = apiinterface.registerqueue(temp,taskbody); call.enqueue(new callback<responsebody>() { @override public void onresponse(call<responsebody> call, response<responsebody> response) { if(response.issuccessful()) { log.d(tag, "signun successful: "); toast.maketext(queueactivity.this, "signup successful", toast.length_long).show(); onqueuesuccess(); }else{ onqueuefailed(); // code ends , doesn't add api database } } @override public void onfailure(call<responsebody> call, throwable t) { log.d(tag,"went wrong",t); toast.maketext(queueactivity.this, "something went wrong", toast.length_long).show(); onqueuefailed(); } });
error: there no syntax error. problem not being able add backend.
p.s: on post man can add achieve adding authorization: bearer <token here>
in header , request task: <task here>
in form data
example task: mobile
update log
i/openglrenderer: initialized egl, version 1.4 w/openglrenderer: failed choose config egl_swap_behavior_preserved, retrying without... d/loginactivity: login d/loginactivity: send request fired d/loginactivity: log in successfull: d/loginactivity: token in intent: eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyjzdwiiojcsimlzcyi6imh0dha6xc9clze5mi4xnjgums4ymjo4mdawxc9hcglcl2f1dghcl2xvz2luiiwiawf0ijoxndkxnty3ndewlcjlehaioje0ote1nzewmtasim5izii6mtq5mtu2nzqxmcwianrpijoiyzrhmtm5yzkxzdbkzdnmmgm3mduwmji3odc3ytq5ymuifq.9sa6d3o0owd3fcg-i3lr2n1nbj8r0xj2wc3zz6i1l2k i/textinputlayout: edittext added not textinputedittext. please switch using class instead. v/renderscript: 0xa9fbb000 launching thread(s), cpus 2 e/surface: getslotfrombufferlocked: unknown buffer: 0xaa004f40 e/surface: getslotfrombufferlocked: unknown buffer: 0xaa004b50 e/surface: getslotfrombufferlocked: unknown buffer: 0xaa005090 d/queueactivity: send network request: task: mobiletest bearer eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyjzdwiiojcsimlzcyi6imh0dha6xc9clze5mi4xnjgums4ymjo4mdawxc9hcglcl2f1dghcl2xvz2luiiwiawf0ijoxndkxnty3ndewlcjlehaioje0ote1nzewmtasim5izii6mtq5mtu2nzqxmcwianrpijoiyzrhmtm5yzkxzdbkzdnmmgm3mduwmji3odc3ytq5ymuifq.9sa6d3o0owd3fcg-i3lr2n1nbj8r0xj2wc3zz6i1l2k e/surface: getslotfrombufferlocked: unknown buffer: 0xaa004fb0
** update okhttpinterceptor log**
04-07 16:00:11.614 24583-25144/com.example.comunicate d/okhttp: --> post http://192.168.1.22:8000/api/auth/queue/store http/1.1 04-07 16:00:11.614 24583-25144/com.example.comunicate d/okhttp: content-type: multipart/form-data; boundary=958f2f27-e6d5-453b-836d-d2ef8073ef5a 04-07 16:00:11.615 24583-25144/com.example.comunicate d/okhttp: content-length: 232 04-07 16:00:11.616 24583-25144/com.example.comunicate d/okhttp: authorization: bearer eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyjzdwiiojcsimlzcyi6imh0dha6xc9clze5mi4xnjgums4ymjo4mdawxc9hcglcl2f1dghcl2xvz2luiiwiawf0ijoxndkxnty5otkwlcjlehaioje0ote1nzm1otasim5izii6mtq5mtu2otk5mcwianrpijoin2zmywzhm2zmmtjiotm3y2rhnmvknjayyzc4zjk3zwuifq.juoxvj80nr9lmjcvjqwsermjd-jotg_w9uhs232lezy 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: --958f2f27-e6d5-453b-836d-d2ef8073ef5a 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: content-disposition: form-data; name="task" 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: content-transfer-encoding: binary 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: content-type: text/plain; charset=utf-8 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: content-length: 6 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: mobile 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: --958f2f27-e6d5-453b-836d-d2ef8073ef5a-- 04-07 16:00:11.618 24583-25144/com.example.comunicate d/okhttp: --> end post (232-byte body) 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: <-- 404 not found http://192.168.1.22:8000/api/auth/queue/store (185ms) 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: host: 192.168.1.22:8000 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: connection: close 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: x-powered-by: php/5.6.30 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: cache-control: no-cache, private 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: content-type: application/json 04-07 16:00:11.804 24583-25144/com.example.comunicate d/okhttp: date: fri, 07 apr 2017 13:00:12 gmt 04-07 16:00:11.813 24583-25144/com.example.comunicate d/okhttp: {"error":{"message":"404 not found","status_code":404}} 04-07 16:00:11.813 24583-25144/com.example.comunicate d/okhttp: <-- end http (55-byte body)
you should intercept requests , add token in header. retrofit supports re-authenticating, in case token expires (which should). following tutorial has need: retrofit — token authentication on android
Comments
Post a Comment