Giter VIP home page Giter VIP logo

hackretrofit's Introduction

HackRetrofit

A hacked edition of Retrofit 2.0.2.

Introductions

This is kind of for fun...

  1. I added two annotations for type and method and apis of retrofit can share the annotations annotated a type:

    @FixField: Provide a fixed field to be posted with other dynamically passed in fields. @GeneratedField: Generated a field when a post request will be made.

  2. Add new support of Mock Server. You can simply use the raw response data to mock a server response like this:

        @Override public Call<List<Contributor>> contributors(String owner, String repo) {
          return delegate.returningRawResponse(getJsonResponse()).contributors(owner, repo);
        }
        private String getJsonResponse(){
            // Eg. [{"login":"Jake", "contributions":1234},{"login":"John", "contributions":1234}]
            return ...;
        }
  3. Add support of TypedFile. So when you want to upload a file, simply do it like this:

    public interface FileUploadService {
      @Multipart
      @POST("upload")
      Call<ResponseBody> upload(@Part("description") RequestBody description,
                                @Part TypedFile typedFile);
    }
    
    ...
    
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://www.println.net/")
        .addConverterFactory(new TypedFileMultiPartBodyConverterFactory())
        .addConverterFactory(GsonConverterFactory.create())
        .build();
     
    FileUploadService service = retrofit.create(FileUploadService.class);
    TypedFile typedFile = new TypedFile("aFile", filename);
    String descriptionString = "This is a description";
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), descriptionString);
     
    Call<ResponseBody> call = service.upload(description, typedFile);
    call.enqueue(...);

    Name of the file to be uploaded will be used to retrieve the MediaType if you don't explictly set it. The retrieve of MediaType is still uncompleted.

More Infomations:

Android 下午茶:Hack Retrofit 之 增强参数.

Android 下午茶:Hack Retrofit (2) 之 Mock Server

深入浅出 Retrofit,这么牛逼的框架你们还不来看看?

hackretrofit's People

Contributors

bennyhuo avatar

Watchers

 avatar  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.