Android Retrofit 2.0 Custom Jsonobject Converter

Source: Internet
Author: User

If in the process of use, do not need gson and other converters, but simply return jsonobject, then how to deal with it?

By reading source discovery, you can manipulate it by customizing the converter:

import retrofit.Call/*Retrofit 2.0*/public interfase ApiService{    @POST("/list")    Call<JSONObject> loadRepo();}

Synchronous operation:

Callcall = service.loadRepo();call.excute()

Asynchronous Operation:

Call<JSONObject> call = service.loadRepo();call.enqueue(new Callback<JSONObject>(){    @Override    publicvoidonResponse(Response<JSONObject> response){        //从response.body()中获取结果    }    @Override    publicvoidonFailure(Throwable t){    }});

Is that it? No.

    • Add Custom Converter

Address: Https://github.com/brokge/Retrofit2.0-JSONCoverter
Select the appropriate version to add to the project. (Retrofit 2.0-beta2 and Retrofit 2.0-beta4 are processed differently)

    • Gsonconverterfactory.create (Gson) change to Jsonconverterfactory.create ()

The complete code is as follows:

private static Retrofit Initretrofit () {okhttpclient httpClient = new Okhttpclient ();if (buildconfig. DEBUG) {Httplogginginterceptor logging = new Httplogginginterceptor ();Logging. SetLevel(Httplogginginterceptor. level. BODY);HttpClient = new Okhttpclient. Builder(). Addinterceptor(logging). Build();} return new Retrofit. Builder(). BaseUrl(Baseutil. Getapiurl()). Addconverterfactory(jsonconverterfactory. Create()). Client(httpClient). Build();}

Perhaps you are also interested in the following articles:
Android Retrofit Request string (non-JSON data)

Android Retrofit 2.0 Custom Jsonobject Converter

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.