Skip to content

dtboy1995/android-sex-http

Repository files navigation

android-sex-http

android-sex-http Build Status

✈️ android async http simple wrapper with cache policy

install

implementation 'org.ithot.android.transmit:http:0.3.2'

usage

  • permissions

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • jsonserializer

implementation 'org.ithot.android.serializer:gson:1.0.1'
  • sample

public class Dummy {
  public String id;
}
// init once
Req.init(context, new JSON());
// send request
Req.build(context)
   .url("https://ithot.org/dummy")
   .res(new Res<Dummy>(){
     @Override
     public void ok(Header[] headers, Dummy response) {

     }

     @Override
     public void no(Header[] headers, String error) {
     }
   })
   .go();
  • config

// default http 80 https 443
Req.init(context, serialier);
// set http port
Req.init(context, 3000, serialier);
// set https port
Req.init(context, 3000, 5000, serialier)
// set base url example for https://api.somedomain.com
Req.base("https://ithot.org");
// distinguish different users request
Req.prefix("user_id");
// if true log response default false
Req.debug(true);
// http lifecycle hooks
Req.hook(new IHTTPHook(){
  // no network call this hook
  @Override
  public void disconnected(Context context) {

  }
  // you can set common headers
  @Override
  public List<Header> headers() {

  }
  // before request call this hook, you can display a dialog
  @Override
  public void pre(Context context) {

  }
  // request done call this hook, you can dismiss a dialog
  @Override
  public void post(Context context) {

  }
  // abnormal response call this hook
  @Override
  public void fail(Header[] headers, String response, Context context) {

  }
});
  • custom json serializer

public class Serializer extends JSONSerializer {
    // you can use any serialization library such as Gson Fastjson etc example below
    private Gson gson = new Gson();

    @Override
    public Object parse(String json, Type type) {
        return gson.fromJson(json, type);
    }

    @Override
    public String stringify(Object object) {
        return gson.toJson(object);
    }
}
Req.init(context, new Serializer());
  • download (break restoration)

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.mp3");
Req.build(context)
   .base(false)
   .url("https://ithot.org/music")
   .res(new FileRes() {
        @Override
        public void done(File f) {
        }

        @Override
        public void undone() {
        }

        @Override
        public void progress(int rate) {
        }

    })
  .download(file);