//依赖
implementation 'com.squareup.retrofit2:retrofit:2.0.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okio:okio:1.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
//权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
//创建Api类
public class Api {
//http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
public static final String wealUrl ="http://gank.io/api/";
//http://www.93.gov.cn/93app/data.do?channelId=1&startNum=1
public static final String newsUrl ="http://www.93.gov.cn/93app/";
//http://172.17.8.100/small/user/v1/login
public static final String loginUrl ="http://172.17.8.100/";
}
//创建接口ApiService
public interface ApiService {
//http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
@GET("data/%E7%A6%8F%E5%88%A9/10/{page}")
Call<WealInfo> getWeal(@Path("page") int page);
//http://www.93.gov.cn/93app/data.do?channelId=1&startNum=1
@GET("data.do")
Call<NewsInfo> getNews(@Query("channelId") int channelId, @Query("startNum") int startNum);
//http://172.17.8.100/small/user/v1/login
//登录post请求
@POST("small/user/v1/login")
@FormUrlEncoded
Call<LoginInfo> login(@Field("phone") String phone, @Field("pwd") String pwd);
//上传头像
}
//activity
public class MainActivity extends AppCompatActivity {
private int page = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getServerData();
// getNewsData();
login();
}
private void getServerData() {
//创建Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.wealUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
//java动态代理的方式得到代理对象
ApiService apiService = retrofit.create(ApiService.class);
//通过代理对象执行get请求返回Call
Call<WealInfo> call = apiService.getWeal(page);
call.enqueue(new Callback<WealInfo>() {
@Override
public void onResponse(Call<WealInfo> call, Response<WealInfo> response) {
WealInfo wealInfo = response.body();
Log.i("xxx", wealInfo.results.size() + "");
}
@Override
public void onFailure(Call<WealInfo> call, Throwable t) {
}
});
}
private void getNewsData() {
//创建Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.newsUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
//java动态代理的方式得到代理对象
ApiService apiService = retrofit.create(ApiService.class);
//通过代理对象执行get请求返回Call
Call<NewsInfo> call = apiService.getNews(1, 1);
call.enqueue(new Callback<NewsInfo>() {
//此方法在主线程
@Override
public void onResponse(Call<NewsInfo> call, Response<NewsInfo> response) {
NewsInfo newsInfo = response.body();
NewsInfo.DataInfo dataInfo = newsInfo.data.get(0);
String title = dataInfo.getTITLE();
Log.i("xxx", title);
}
@Override
public void onFailure(Call<NewsInfo> call, Throwable t) {
}
});
}
private void login() {
//创建日志拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message);
}
});
//设置日志拦截器模式
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//创建OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
//创建Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.loginUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
//java动态代理的方式得到代理对象
ApiService apiService = retrofit.create(ApiService.class);
//通过代理对象执行get请求返回Call
Call<LoginInfo> call = apiService.login("18810609754", "123");
call.enqueue(new Callback<LoginInfo>() {
@Override
public void onResponse(Call<LoginInfo> call, Response<LoginInfo> response) {
LoginInfo loginInfo = response.body();
String nickName = loginInfo.result.getNickName();
Log.i("xxx", nickName);
}
@Override
public void onFailure(Call<LoginInfo> call, Throwable t) {
}
});
}
}
三个接口的Bean类
//福利的Bean类
public class WealInfo {
public List<resultsInfo> results;
public class resultsInfo {
public String url;
public String who;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
}
}
//新闻的bean类
public class NewsInfo {
public List<DataInfo> data;
public class DataInfo {
public String TITLE;
public String IMAGEURL;
public String getTITLE() {
return TITLE;
}
public void setTITLE(String TITLE) {
this.TITLE = TITLE;
}
public String getIMAGEURL() {
return IMAGEURL;
}
public void setIMAGEURL(String IMAGEURL) {
this.IMAGEURL = IMAGEURL;
}
}
}
//登录的bean类
public class NewsInfo {
public List<DataInfo> data;
public class DataInfo {
public String TITLE;
public String IMAGEURL;
public String getTITLE() {
return TITLE;
}
public void setTITLE(String TITLE) {
this.TITLE = TITLE;
}
public String getIMAGEURL() {
return IMAGEURL;
}
public void setIMAGEURL(String IMAGEURL) {
this.IMAGEURL = IMAGEURL;
}
}
}