Git Product home page Git Product logo

okhttprequest's Introduction

okhttpRequest

okhttp with coroutines.

说明

在build.gradle中引入

implementation 'com.github.sun00743.okhttpRequest:request:1.0.0'

使用kotlin提供的协程库以及okhttp,简单封装了一下,让协程来负责请求网络(Dispatchers.IO)及线程转换。

使用gson来提供json format。

使用

1.初始化,首先在Application或者什么地方先初始化,像使用okhttp一样,构建okhttpclient,然后把Client当作参数传入Connector

        val okHttpClient = OkHttpClient.Builder()
                //...
                .build()
        Connector.init(okHttpClient)

2.发送请求 execute 方法传递CoroutineScope进去。官方提供lifecycleScope是不错的选择,毕竟通常我们关掉Activity的同时也想干掉请求。

GetRequester(url, StringParser())
                .success {
                    text_result.text = it
                }
                .error { msg, code ->
                    text_result.text = msg
                }
                .execute(lifecycleScope)

//...

okhttprequest's People

Contributors

sun00743 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.