RxJava2 + movie fit: simple to use and POST-based request encapsulation tool class, rxjava2movie fit

Source: Internet
Author: User

RxJava2 + movie fit: simple to use and POST-based request encapsulation tool class, rxjava2movie fit

First, dependency

// Android supports Rxjava // pay attention to the use of RxJava2 compile 'IO. reactivex. rxjava2: rxjava: 2.0.1 'compute' io. reactivex. rxjava2: rxandroid: 2.0.1 '// Android supports using fitcompile 'com. squareup. invalid fit2: Invalid fit: 2.1.0 '// connect to Alibaba fit & RxJava // be sure to use the RxJava2 version compile 'com. jakewharton. invalid fit: retrofit2-rxjava2-adapter: 1.0.0 '// Gson parsing compile' com. squareup. unzip fit2: converter-gson: 2.1.0 '// parses it into Stringcompile 'com. squareup. export fit2: converter-scalars: 2.0.0 'compile files ('libs/fastjson. jar ')

Fastjson is required for my own projects, and it is also required for my own projects to parse it into a String type.

Write interface first

public interface GetRequestInterface {    @POST(".")    Observable
 
   getCall(@Body JSONObject parms);    }
 

All company projects are post requests. Therefore, a tool class is encapsulated. You only need to call the input parameters directly.

Callback Interface

Public interface HttpRequestUtils {/*** request data in post mode * @ param mContext Context * @ param url address * @ param parmsJson params parameter * @ param listener */void loadingDataPost (Context mContext, string url, JSONObject parmsJson, OnLoadDataListener listener); public interface OnLoadDataListener {/*** data normal callback *** @ param flag indicates whether the request is successful, the success here is the success and failure in our project interface * @ param result is successful, data is returned, and the Failure Returns the reason */void onLoadCallBack (boolean flag, String result ); /*** callback when an exception occurs. * The effect is similar to that of the flag = false method in the preceding interface, however, this failure is the cause of access level failure ** @ param errorStr failure */void onError (String errorStr );}}

Tool

/*** Created by guoyan on requests /6/1 * encapsulation type post request for request data */public class OkData {/*** request Network Method * @ param url request url * @ param the JSONObject object of the parmsJson request parameter fasejson * @ param listener callback */public void requesData (String url, JSONObject parmsJson, final HttpRequestUtils.OnLoadDataListener listener) {OkHttpClient.Builder httpClient = new OkHttpClient.Builder (); httpClient. addInterceptor (new Interceptor () {@ Override public Response intercept (Interceptor.Chain chain) throws IOException {Request original = chain. request (); Request request = original. newBuilder (). addHeader ("key ",EncoderByMd5()). AddHeader ("version", "1.5.1 "). addHeader ("application/json", "charset = UTF-8 "). method (original. method (), original. body ()). build (); return chain. proceed (request) ;}}); OkHttpClient client = httpClient. build (); fit = new fit.Builder (). baseUrl (url). client (client) // String Parsing. addConverterFactory (ScalarsConverterFactory.Create() // Supports gson. addConverterFactory (GsonConverterFactory.Create() // Supports rejava2. addCallAdapterFactory (RxJava2CallAdapterFactory.Create(). Build (); GetRequestInterface requst = fit. create (GetRequestInterface. class); Observable
 
  
Call = requst. getCall (parmsJson); call. subscribeOn (Schedulers.
  Io(). ObserveOn (AndroidSchedulers.
  MainThread(). Subscribe (new Observer
  
   
() {@ Override public void onSubscribe (Disposable d) {// start request} @ Override public void onNext (String value) {// request data if (listener! = Null) {listener. onLoadCallBack (true, value) ;}@override public void onError (Throwable e) {// request failure if (listener! = Null) {listener. onError (e. toString () ;}@override public void onComplete (){}});}}
  
 
 

Use

Private void initData () {JSONObject parmsJson = new JSONObject (); parmsJson. put ("client", ""); parmsJson. put ("blackBox", ""); parmsJson. put ("token", ""); parmsJson. put ("type_id", ""); parmsJson. put ("product_id", ""); parmsJson. put ("ruleSize", 7); OkData data = new OkData (); data. requesData (url, parmsJson, new HttpRequestUtils.OnLoadDataListener () {@ Override public void onLoadCallBack (boolean flag, String result) {Log.E("Request succeeded", result) ;}@ Override public void onError (String errorStr) {Log.E("Request failed", errorStr );}});}

You only need to call this method for each request network. You can input parameters and customize the parameters as needed.

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.