OkHttp封装:OkHttpPlus

jopen 9年前

OkHttp封装,支持GET、POST、UI线程回调、JSON格式解析、链式调用、小文件上传下载及进度监听等功能
</div>

OkHttpPlus is a tool for OkHttp


OkHttpPlus是OkHttp的一个工具类,主要对Get和Post方法进行了简单封装,所有方法的回调都在UI线程完成,内置了 String、JsonObject、JsonArray数据类型的解析器,封装了对小文件下载和文件上传功能,可以实现进度监听,使之满足常见的 Http需求。

该项目暂时处于测试阶段,暂时不要应用到生产环境中,请等待之后的正式版发布。

如果满足不了你的需求,请发issuse。

中文文档请戳这里

Features

  • More simple use for GET and POST
  • CallBack run on the UI Thread
  • Support small size file download and upload without other library
  • Build in support for JSON Object and JSON Array parse

Sample Usage

Init OkHttpClient

First , you can init the OkHttpClient as usual

public class OkApplication extends Application {        @Override      public void onCreate() {          super.onCreate();          OkHttpClient okHttpClient = OkHttpProxy.getInstance();          okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);          okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);          okHttpClient.setWriteTimeout(15, TimeUnit.SECONDS);    }  }

Get JSON Object And JSON Array

Use OkJsonParser ,you could get a User Object form json format string.

OkHttpProxy.get()                  .url(URL_USER)                  .tag(this)                  .execute(new OkCallback<User>(new OkJsonParser<User>() {                  }) {                      @Override                      public void onSuccess(int code, User user) {                          tv_response.setText(user.toString());                      }                        @Override                      public void onFailure(Throwable e) {                          tv_response.setText(e.getMessage());                      }                  });

项目主页:http://www.open-open.com/lib/view/home/1448417937642