All Projects → xwdz → OkHttpUtils

xwdz / OkHttpUtils

Licence: other
【Deprecated】Android 方便,简洁的OKHttp工具类,支持Model自动解析Json,多图片、多文件上传

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to OkHttpUtils

SunnyBeach
阳光沙滩APP
Stars: ✭ 60 (+114.29%)
Mutual labels:  gson, okhttp
Okhttp Volley Gson
Sample app showcasing Android Networking with OkHttp, Volley and Gson. Check out the article to further explanation.
Stars: ✭ 235 (+739.29%)
Mutual labels:  gson, okhttp
Kriptofolio
Free open source minimalistic cryptocurrencies portfolio app for Android.
Stars: ✭ 79 (+182.14%)
Mutual labels:  gson, okhttp
MrTranslator
📘MrTranslator-A Translation Android APP
Stars: ✭ 16 (-42.86%)
Mutual labels:  gson, okhttp
Easyhttp
Android 网络请求框架,简单易用,so easy
Stars: ✭ 423 (+1410.71%)
Mutual labels:  gson, okhttp
Zsbpads
Material design、MVC、OKHttp、Glide、greendao 设计的一款面试题题库的app完整项目
Stars: ✭ 25 (-10.71%)
Mutual labels:  gson, okhttp
Easygank
💊 The project build framework based on the Rx series and MVP pattern.
Stars: ✭ 750 (+2578.57%)
Mutual labels:  gson, okhttp
Mvpframes
整合大量主流开源项目并且可高度配置化的 Android MVP 快速集成框架,支持 AndroidX
Stars: ✭ 100 (+257.14%)
Mutual labels:  gson, okhttp
RestaurantsExplorer
Android application build with MVVM Pattern, using Zomato API to enable search cities arround the world and display the city restaurants on a map.
Stars: ✭ 32 (+14.29%)
Mutual labels:  okhttp
HTTP-Wrapper
A simple http wrapper
Stars: ✭ 13 (-53.57%)
Mutual labels:  gson
Jsontokotlinclass
🚀 Plugin for Android Studio And IntelliJ Idea to generate Kotlin data class code from JSON text ( Json to Kotlin )
Stars: ✭ 2,438 (+8607.14%)
Mutual labels:  gson
fx-gson
A set of type adapters for Google Gson to make JavaFX properties serialization more natural
Stars: ✭ 53 (+89.29%)
Mutual labels:  gson
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (+428.57%)
Mutual labels:  gson
Gsonfactory
Gson 解析容错框架,愿从此再无 Json 解析报错
Stars: ✭ 188 (+571.43%)
Mutual labels:  gson
weather
基于MVP的安卓天气demo
Stars: ✭ 49 (+75%)
Mutual labels:  okhttp
Trailersapp
A simple demo project for The Movie DB based on MVVM clean architecture.
Stars: ✭ 180 (+542.86%)
Mutual labels:  gson
methanol
⚗️ Lightweight HTTP extensions for Java
Stars: ✭ 172 (+514.29%)
Mutual labels:  gson
spring-rest-2-ts
spring rest 2 ts is typescript generator which produces data model and services in typescript based on Spring MVC annotations. It supports generation for Angular and React
Stars: ✭ 59 (+110.71%)
Mutual labels:  gson
RAD
Mvp+Databinding+Aop+Rx
Stars: ✭ 27 (-3.57%)
Mutual labels:  okhttp
moshi-gson-interop
An interop tool for safely mixing Moshi and Gson models in JSON serialization.
Stars: ✭ 39 (+39.29%)
Mutual labels:  gson

特性

  • 支持自动解析JSON返回实体类
  • 可随时取消某个请求或者全部请求
  • 支持上传多文件
  • 支持上传混合参数(文件参数以及json参数)
  • 线程默认回调再主线程(.setCallbackMainUIThread(true /* 默认为true */))
  • 支持文件下载

$lastVersion =

添加依赖


compile 'com.xwdz:okHttpUtils:$lastVersion'
compile 'com.squareup.okhttp3:okhttp:3.5.0'

注意

一定要保证.execute(new xxxCallback)方法最后调用

提供两种获取QuietHttp方法

1. 直接实例化使用内置配置


默认配置如下:

   static {
           HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLog(TAG));
           logInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
           sOkHttpClient = new OkHttpClient.Builder()
                   .writeTimeout(20, TimeUnit.SECONDS)
                   .readTimeout(20, TimeUnit.SECONDS)
                   .addInterceptor(logInterceptor)
                   .writeTimeout(20, TimeUnit.SECONDS).build();
   }
    
    
2.  传入自定义OkHttpClient

QuietOkHttp.setOkHttpClient(OkHttpClient okHttpClient);

请求

Get

 QuietOkHttp.get("https://api.github.com/search/users")
     .tag(MainActivity.class.getName())
     .addParams("q", "a")
     .addParams("page", "1")
     .addParams("per_page", "10")
     // 回调在子线程
     .setCallbackMainUIThread(false)
     .execute(new JsonCallBack<Response<List<User>>>() {
         @Override
         public void onSuccess(Call call, Response<List<User>> response) {
             for (User item : response.items) {
                 Log.e("XHttp", "res = " + item.toString());
             }
         }

         @Override
         public void onFailure(Call call, Exception e) {

         }
     });

POST

 QuietOkHttp.post(URL)
     .tag(MainActivity.class.getName())
     .addParams("q", "xwdz")
     .addParams("page", "1")
     .addParams("per_page", "10")
     .execute(new JsonCallBack<Response<List<User>>>() {
         @Override
         public void onSuccess(Call call, Response<List<User>> response) {
             for (User item : response.items) {
                 Log.e("XHttp", "res = " + item.toString());
             }
         }

         @Override
         public void onFailure(Call call, Exception e) {

         }
     });

POST 文件

        List<File> list = new ArrayList<>();
        list.add("file", file);
    
        QuietOkHttp.postFile(URL)
                .uploadFile("files",list)
                .tag(mTag)
                .execute(new StringCallBack() {
                    @Override
                    protected void onSuccess(Call call, String response) {
                        Log.e("TAG", "res:" + response);
                    }
    
                    @Override
                    public void onFailure(Call call, Exception e) {
                        e.printStackTrace();
                    }
                });

POST文件 + 混合参数


         QuietOkHttp.postFile(URL_UPLOAD)
                .uploadFile("files", list)
                .addParams("key","10926a9165054566b6df6a8410e45f08")
                .addParams("address", "sugar 办公室")
                .addParams("desc", "测试工具测试")
                .execute(new StringCallBack() {
                    @Override
                    protected void onSuccess(Call call, String response) {
                        Log.e("TAG", "response:" + response);
                    }

                    @Override
                    public void onFailure(Call call, Exception e) {
                        Log.e("TAG", "onFailure:" + e.toString());
                    }
                });

取消一个请求


QuietOkHttp.cancel(tag)

取消全部请求

QuietOkHttp.cancelAll()

默认支持Callback如下

  • StringCallBack
  • JsonCallBack
  • FileCallBack

如需要其他解析扩展,继承AbstractCallBack 即可

比如

public abstract class StringCallBack extends AbstractCallBack<String> {

    @Override
    protected String parser(Call call, Response response) throws IOException {
        final String result = response.body().string();
        onSuccess(call, result);
        return result;
    }

    protected abstract void onSuccess(Call call, String response);
}

混淆


#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
#okhttpUtils
-dontwarn com.xwdz.http.**
-keep class com.xwdz.http.**{*;}

# gson
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].