android - Retrofit 2.0 POST method with body is String -


this question may have been asked before new version 2.0 did not find correct answer yet.

my problem below:

public interface  authenticationapi {      @post("token")     call<string> authenticate(@body string  body);  } 

then call:

retrofit retrofit = new retrofit.builder().baseurl(authenurl)                 .addconverterfactory(gsonconverterfactory.create())                 .build();         authenticationapi authen = retrofit.create(authenticationapi.class);           try {             call<string> call1 = authen.authenticate(authenbody);             call1.enqueue(new callback<string>() {                 @override                 public void onresponse(response<string> response, retrofit retrofit) {                     log.d("thaipd", "success " + response.raw().message());                 }                  @override                 public void onfailure(throwable t) {                     log.e("thaipd", " fail " + t.getmessage());                 }             });         } catch (exception e) {             log.e("thaipd", e.getmessage());             e.printstacktrace();         } 

then receive response protocol=http/1.1, code=400, message=bad request, means body parameter not correct.

when try make request other tool postman correct result code 200.

i found this answer retrofit 2.0 can not find typedstring class.

here result when try tool (dhc) enter image description here

update:
retrofit2.0 has own converter-scalars module string , primitives (and boxed).

com.squareup.retrofit2:converter-scalars 

you can write custom converter , retrofit repository has own string converter implementation sample: tostringconverterfactory

class tostringconverterfactory extends converter.factory {   private static final mediatype media_type = mediatype.parse("text/plain");    @override   public converter<responsebody, ?> fromresponsebody(type type, annotation[] annotations) {     if (string.class.equals(type)) {       return new converter<responsebody, string>() {         @override public string convert(responsebody value) throws ioexception {           return value.string();         }       };     }     return null;   }    @override public converter<?, requestbody> torequestbody(type type, annotation[] annotations) {     if (string.class.equals(type)) {       return new converter<string, requestbody>() {         @override public requestbody convert(string value) throws ioexception {           return requestbody.create(media_type, value);         }       };     }     return null;   } } 

and related issue tracked here.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -