android - Whats wrong with this retrofit call? -


im trying send post , build response using retrofit android.

i have managed send no problems need send post body elements.

public static <s> s createaccessservice(class<s> serviceclass, string code, string redirecturi,                                   string clientid, string clientsecret) {     okhttpclient.builder httpclient = new okhttpclient.builder();     string basiccredentials = clientid+":"+clientsecret;     byte[] encodebytes = base64.encode(basiccredentials.getbytes(), base64.no_wrap);     httpclient.addinterceptor(new interceptor() {         @override         public response intercept(chain chain) throws ioexception {             request original = chain.request();             requestbody body = new formbody.builder()                     .add("grant_type", "authorization_code")                     .add("code", code)                     .add("redirect_uri", redirecturi).build();             request request = original.newbuilder()                     .addheader("authorization", "basic "+new string(encodebytes))                     .method(original.method(), original.body())                     .put(body)                     .build();             return chain.proceed(request);         }     });      okhttpclient client = httpclient.build();     retrofit retrofit = new retrofit.builder()             .baseurl(api_base_url)             .addconverterfactory(gsonconverterfactory.create())             .client(client)             .build();      return retrofit.create(serviceclass); 

the post i'm trying build looks one:

post /api/token http/1.1 host: accounts.spotify.com authorization: basic *********************************** cache-control: no-cache postman-token: 99177da6-1606-3145-689d-bc4b09b3f212 content-type: application/x-www-form-urlencoded  grant_type=authorization_code&redirect_uri=http%3a%2f%2flocalhost%3a8888%2fcallback&code********************************************************************************************************************************************************* 

obviously keys hidden security.

and pojo im using store response.

public class session {  private string scope;  private string expires_in;  private string token_type;  private string refresh_token;  private string access_token; private string error;  public string geterror_description() {     return error_description; }  public void seterror_description(string error_description) {     this.error_description = error_description; }  public string geterror() {     return error; }  public void seterror(string error) {     this.error = error; }  private string error_description;  public string getscope () {     return scope; }  public void setscope (string scope) {     this.scope = scope; }  public string getexpires_in () {     return expires_in; }  public void setexpires_in (string expires_in) {     this.expires_in = expires_in; }  public string gettoken_type () {     return token_type; }  public void settoken_type (string token_type) {     this.token_type = token_type; }  public string getrefresh_token () {     return refresh_token; }  public void setrefresh_token (string refresh_token) {     this.refresh_token = refresh_token; }  public string getaccess_token () {     return access_token; }  public void setaccess_token (string access_token) {     this.access_token = access_token; }     } 

i have changed lot of things no matter what, response cannot instanciate new session object, returning null always.

added:

this interface im using:

   @formurlencoded    @post("/api/token")    call<session> getsession(); 

what doing wrong?

thanks

well, if try send post request retrofit2, should try this:

first, create interface, in interface declare body contents post request:

public interface isesion {      @formurlencoded     @post("/users/login")     call<sesion> insesion(@field("username") string usuario, @field("password") string password); } 

the parameter @field body content use in post request

now, can make in mainactivity:

public class mainactivity extends appcompatactivity {     private retrofit retrofit; //your retrofit object     private isesion isesion; //your interface      @override     protected void oncreate(bundle savedinstancestate) {     retrofit = new retrofit.builder()             .baseurl(getstring(r.string.url_base)) //your base url ("http://www.myexample.com")             .addconverterfactory(gsonconverterfactory.create())             .build();      isesion = retrofit.create(isesion.class);     call<sesion> call = isesion.insesion(usuario, contrasena); //you fields edittext, example, , pass method in interface         call.enqueue(new callback<sesion>() {             @override             public void onresponse(call<sesion> call, response<sesion> response) {                 code = response.code();                 switch (code) {                     case 200:                         intent intent = new intent(getactivity(), mainactivity.class);                         intent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task);                         startactivity(intent);                          break;                     case 401:                         error = response.body().iserror();                         mensaje = response.body().getmensaje();                         if (error) {                             snackbar.make(view, mensaje, snackbar.length_long).show();                         }                         break;                 }             }              @override             public void onfailure(call<sesion> call, throwable t) {                 t.printstacktrace();                 snackbar.make(view, getstring(r.string.error_general), snackbar.length_long).show();             }         });     } } 

the model used in method call<sesion> next:

public class sesion {      @serializedname("error")     private boolean error;      @serializedname("message")     private string mensaje;      public boolean iserror() {         return error;     }      public string getmensaje() {         return mensaje;     } }   

edit:

if want send token in body content couldtry this:

call<sesion> call = isesion.insesion("basic " + generatedtoken); // here authentication token         call.enqueue(new callback<sesion>() {             @override             public void onresponse(call<sesion> call, response<sesion> response) {                 code = response.code();                 switch (code) {                     case 200:                         intent intent = new intent(getactivity(), mainactivity.class);                         intent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task);                         startactivity(intent);                          break;                     case 401:                         error = response.body().iserror();                         mensaje = response.body().getmensaje();                         if (error) {                             snackbar.make(view, mensaje, snackbar.length_long).show();                         }                         break;                 }             }              @override             public void onfailure(call<sesion> call, throwable t) {                 t.printstacktrace();                 snackbar.make(view, getstring(r.string.error_general), snackbar.length_long).show();             }         });     } 

and interface:

public interface isesion {      @formurlencoded     @post("/users/login")     call<sesion> insesion(@field("token") string generatedtoken); } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -