After using the retrofit as the network request library, the author consulted the related tutorials on the net, all of which are request JSON data, using Addconverterfactory (Gsonconverterfactory.create ()) as the converter, If the business requirement is a request string, not a JSON data format, continuing to use the converter generates an error.
OK, come to the chase, how to use retrofit to request a string:
Visit Official documents: http://square.github.io/retrofit/
There are the following descriptions:
In fact, the official has provided a string converter, then the next to import it into the project to use it.
Add Gradle Dependency
‘com.squareup.retrofit2:converter-scalars:2.0.0‘
Using the Scalarsconverterfactory Converter
Public classRetrofitmanager {PrivateRetrofit Mretrofit;Private volatile StaticRetrofitmanager instance;Private Retrofitmanager() {Mretrofit =NewRetrofit.builder (). BASEURL (Httpaddress.site). Addconverterfactory (scalarsconverterfactory.c Reate ()). build (); } Public StaticRetrofitmanagergetinstance() {if(Instance = =NULL) {synchronized (Retrofitmanager.class) {if(Instance = =NULL) {instance =NewRetrofitmanager (); } } }returnInstance } PublicRetrofitGetretrofit() {if(Mretrofit = =NULL) {Mretrofit = Retrofitmanager.getinstance (). Initretrofit (); }returnMretrofit; }PrivateRetrofitInitretrofit() {returnMretrofit; }}
Create a request interface
Public interface mallrequest { @Headers({"User-agent:android"})@GET("{Controller}/{method}") Call<string> GetResult (@Path("Controller") String Controller,@Path("Method") String method,@Query("Api_token") String Api_token);@GET("{Controller}/{method}") call<string> Getadvertresult (@Path("Controller") String Controller,@Path("Method") String method,@Query("Api_token") String Api_token);}
Request data
PrivateMallrequest mmallrequest; Mmallrequest = Retrofitmanager.getinstance (). Getretrofit (). Create (Mallrequest.class);Pager<String>Pager= Mmallrequest.getadvertresult ("Site","Adv-slide", Lepaapitoken.gettoken ("Site","Adv-slide"));Pager. Enqueue (Newcallback<String> () {@Override Publicvoid Onresponse (Pager<String>Pager, Retrofit2.Response<String>Response) {Log. D ("Debug",Response. body ()); } @Override Publicvoid OnFailure (Pager<String>Pager, Throwable t) {}});
Results:
Done!
Thank Shing for sharing, the original link:
http://blog.csdn.net/u013003052/article/details/50992436
Android Retrofit Request string (non-JSON data)