WASP

2017-03-28  本文已影响26人  mimimomo

title: WASP

https://github.com/orhanobut/wasp

wasp一个紧凑且易于使用的“一体化”网络解决方案。


问题

谈到日常发展,您不仅需要一个库来处理网络,你需要处理模拟调用,使用多个端点,处理证书和cookie和许多其他boiler plate code。用WASP,你可以轻松处理一切。

wasp内部使用:

  1. Volley for the network stack
  2. Gson解析
  3. http堆栈的 OkHttp

wasp提供:

wasp的目标:

添加依赖关系更多信息https://jitpack.io/#orhanobut/wasp/1.15

repositories {
  // ...
  maven { url "https://jitpack.io" }
}

dependencies {
  compile 'com.github.orhanobut:wasp:1.15'
}

创建服务接口

public interface GitHubService {

  // Async call
  @GET("/repos/{id}")
  void getRepo(@Path("id") String id, Callback<Repo> callback);
  
  // Async call with WaspRequest (cancelable)
  @GET("/repos/{id}")
  WaspRequest getRepo(@Path("id") String id, Callback<Repo> callback);
    
  // Rx
  @Mock
  @POST("/repos")
  Observable<Repo> createRepo(@Body Repo repo);
  
  // sync call
  @GET("/users/{id}")
  User getUser(@Path("id") String id);
}

初始化wasp

GitHubService service = new Wasp.Builder(this)
  .setEndpoint("https://api.github.com")
  .setRequestInterceptor                     // Optional
  .trustCertificates                         // Optional
  .setHttpStack                              // Optional
  .enableCookies                             // Optional
  .setNetworkMode(NetworkMode.MOCK)          // Optional(Used for Mock)
  .build()
  .create(GitHubService.class);

可以随意使用了!异步

service.getRepo(id, new Callback<Repo>{

  @Override
  public void onSuccess(Response response, Repo repo) {
    // do something
  }
  
  @Override
  public void onError(WaspError error) {
    // handle error
  }
});

与WaspRequest异步(可取消)

WaspRequest request = service.getRepo(id, new Callback<Repo>{

  @Override
  public void onSuccess(Response response, Repo repo) {
    // do something
  }
  
  @Override
  public void onError(WaspError error) {
    // handle error
  }
});
request.cancel();  //cancels the request

Rx

Observable<Repo> observable = service.createRepo(repo);

Sync 同步

User user = service.getUser(id);

可以去从wiki中获得更多的细节

License

Copyright 2014 Orhan Obut

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

上一篇 下一篇

猜你喜欢

热点阅读