Android Retrofit Request string (non-JSON data)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.