首先看下demo
1、首先定义请求模版接口
public interface IServiceDemo {
@POST("login/nameAuth")
Call<String> login(
@Body User ueser);
}
2、定义
Retrofit类
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://123.56.186.199:8080/map-mobile/")
//增加返回值为String的支持
.addConverterFactory(ScalarsConverterFactory.create())
//增加返回值为Gson的支持(以实体类返回)
.addConverterFactory(GsonConverterFactory.create())
//增加返回值为Oservable<T>的支持
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
3、使用接口获取响应
IServiceDemo requestSerives = retrofit.create(IServiceDemo.class);//这里采用的是Java的动态代理模式
User user = new User();
user.setMobile("18508480922");
user.setPassword("111111");
Call<String> call = requestSerives.login(user);//传入我们请求的键值对的值
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
System.out.println("return:" + response.body().toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
System.out.println("失败");
}
});
这一篇我们先看 Retrofit的创建及接口的创建,下一篇看具体网络请求的执行,及先分析下面执行流程
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://123.56.186.199:8080/map-mobile/")
//增加返回值为String的支持
.addConverterFactory(ScalarsConverterFactory.create())
//增加返回值为Gson的支持(以实体类返回)
.addConverterFactory(GsonConverterFactory.create())
//增加返回值为Oservable<T>的支持
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
IServiceDemo requestSerives = retrofit.create(IServiceDemo.class);//这里采用的是Java的动态代理模式
User user = new User();
user.setMobile("18508480922");
user.setPassword("111111");
Call<String> call = requestSerives.login(user);//传入我们请求的键值对的值
1、首先我们看下Retrofit的创建
public Builder() {
// Add the built-in converter factory first. This prevents overriding its behavior but also
// ensures correct behavior when using converters that consume all types.
converterFactories.add(new BuiltInConverters());
}
Builder是
Retrofit的一个内部类,这里会添加一个BuiltInConverters关于converterFactories后面会讲
public Builder baseUrl(String baseUrl) {
checkNotNull(baseUrl, "baseUrl == null");
HttpUrl httpUrl = HttpUrl.parse(baseUrl);
if (httpUrl == null) {
throw new IllegalArgumentException("Illegal URL: " + baseUrl);
}
return baseUrl(httpUrl);
}
转化为HttpUrl,然后调用baseUrl
public Builder baseUrl(final HttpUrl baseUrl) {
checkNotNull(baseUrl, "baseUrl == null");
List<String> pathSegments = baseUrl.pathSegments();
if (!"".equals(pathSegments.get(pathSegments.size() - 1))) {
throw new IllegalArgumentException("baseUrl must end in /: " + baseUrl);
}
return baseUrl(new BaseUrl() {
@Override public HttpUrl url() {
return baseUrl;
}
});
}
public Builder baseUrl(BaseUrl baseUrl) {
this.baseUrl = checkNotNull(baseUrl, "baseUrl == null");
return this;
}
public Builder addConverterFactory(Converter.Factory factory) {
converterFactories.add(checkNotNull(factory, "factory == null"));
return this;
}
添加converterFactories
public Builder addCallAdapterFactory(CallAdapter.Factory factory) {
adapterFactories.add(checkNotNull(factory, "factory == null"));
return this;
}
添加adapterFactories
最后调用build
public Retrofit build() {
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
okhttp3.Call.Factory callFactory = this.c