依赖:
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwei.retofit.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
package com.bwei.retofit.api;
import com.bwei.retofit.bean.User;
import com.bwei.retofit.bean.UserInfo;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2018/3/12 19:13
*/
public interface ApiService {
/**
* 无参get请求
* http://service.meiyinkeqiu.com/service/ads/cptj
*
* @return
*/
@GET("service/ads/cptj")
Call<User> getNoParmars();
/**
* 有参get请求
* 拼接参数 /形式
*
* @return https://api.github.com/users/baiiu
* 用下面这个接口练 上面接口有问题
* http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
*
*/
@GET("users/{user}")
Call<UserInfo> getHasParmars(@Path("user") String user);
/**
* http://www.93.gov.cn/93app/data.do
* channelId
* startNum
* 拼接 ? &
* 为主
*/
}
package com.bwei.retofit.api;
/**
* 1. 类的用途 把公共部分封装起来
* 2. @author forever
* 3. @date 2018/3/12 19:18
*/
public class Api {
public static String url = "http://service.meiyinkeqiu.com/";
public static String userUrl = "https://api.github.com/";
}
package com.bwei.retofit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.bwei.retofit.api.Api;
import com.bwei.retofit.api.ApiService;
import com.bwei.retofit.bean.User;
import com.bwei.retofit.bean.UserInfo;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getNoParmars();
getHasParmars();
}
private void getNoParmars() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
//使用建造者设计模式
Retrofit retrofit = new Retrofit.Builder().client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Api.url).build();
//通过动态代理模式得到代理对象
ApiService apiService = retrofit.create(ApiService.class);
Call<User> call = apiService.getNoParmars();
call.enqueue(new Callback<User>() {
//回调在主线程
@Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();
List<List<String>> dropdown = user.getDropdown();
List<String> list = dropdown.get(0);
String str = list.get(0);
Log.i("xxx", str);
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
}
private void getHasParmars() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
//使用建造者设计模式
Retrofit retrofit = new Retrofit.Builder().client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Api.userUrl)
.build();
//通过动态代理模式得到代理对象
ApiService apiService = retrofit.create(ApiService.class);
Call<UserInfo> call = apiService.getHasParmars("forever");
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
UserInfo userInfo = response.body();
String name = userInfo.getName();
Log.i("xxx", name);
}
@Override
public void onFailure(Call<UserInfo> call, Throwable t) {
Log.i("xxx",t.getMessage());
}
});
}
}
package com.bwei.retofit.bean;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2018/3/12 19:27
*/
public class UserInfo {
/**
* login : baiiu
* id : 7720476
* avatar_url : https://avatars0.githubusercontent.com/u/7720476?v=4
* gravatar_id :
* url : https://api.github.com/users/baiiu
* html_url : https://github.com/baiiu
* followers_url : https://api.github.com/users/baiiu/followers
* following_url : https://api.github.com/users/baiiu/following{/other_user}
* gists_url : https://api.github.com/users/baiiu/gists{/gist_id}
* starred_url : https://api.github.com/users/baiiu/starred{/owner}{/repo}
* subscriptions_url : https://api.github.com/users/baiiu/subscriptions
* organizations_url : https://api.github.com/users/baiiu/orgs
* repos_url : https://api.github.com/users/baiiu/repos
* events_url : https://api.github.com/users/baiiu/events{/privacy}
* received_events_url : https://api.github.com/users/baiiu/received_events
* type : User
* site_admin : false
* name : Zhe Zhu
* company : null
* blog :
* location : beijing
* email : null
* hireable : true
* bio : heading towards the starry sky,starting from the steady step.
* public_repos : 26
* public_gists : 1
* followers : 47
* following : 84
* created_at : 2014-05-28T04:56:40Z
* updated_at : 2018-02-03T11:04:45Z
*/
private String login;
private int id;
private String avatar_url;
private String gravatar_id;
private String url;
private String html_url;
private String followers_url;
private String following_url;
private String gists_url;
private String starred_url;
private String subscriptions_url;
private String organizations_url;
private String repos_url;
private String events_url;
private String received_events_url;
private String type;
private boolean site_admin;
private String name;
private Object company;
private String blog;
private String location;
private Object email;
private boolean hireable;
private String bio;
private int public_repos;
private int public_gists;
private int followers;
private int following;
private String created_at;
private String updated_at;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAvatar_url() {
return avatar_url;
}
public void setAvatar_url(String avatar_url) {
this.avatar_url = avatar_url;
}
public String getGravatar_id() {
return gravatar_id;
}
public void setGravatar_id(String gravatar_id) {
this.gravatar_id = gravatar_id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getHtml_url() {
return html_url;
}
public void setHtml_url(String html_url) {
this.html_url = html_url;
}
public String getFollowers_url() {
return followers_url;
}
public void setFollowers_url(String followers_url) {
this.followers_url = followers_url;
}
public String getFollowing_url() {
return following_url;
}
public void setFollowing_url(String following_url) {
this.following_url = following_url;
}
public String getGists_url() {
return gists_url;
}
public void setGists_url(String gists_url) {
this.gists_url = gists_url;
}
public String getStarred_url() {
return starred_url;
}
public void setStarred_url(String starred_url) {
this.starred_url = starred_url;
}
public String getSubscriptions_url() {
return subscriptions_url;
}
public void setSubscriptions_url(String subscriptions_url) {
this.subscriptions_url = subscriptions_url;
}
public String getOrganizations_url() {
return organizations_url;
}
public void setOrganizations_url(String organizations_url) {
this.organizations_url = organizations_url;
}
public String getRepos_url() {
return repos_url;
}
public void setRepos_url(String repos_url) {
this.repos_url = repos_url;
}
public String getEvents_url() {
return events_url;
}
public void setEvents_url(String events_url) {
this.events_url = events_url;
}
public String getReceived_events_url() {
return received_events_url;
}
public void setReceived_events_url(String received_events_url) {
this.received_events_url = received_events_url;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isSite_admin() {
return site_admin;
}
public void setSite_admin(boolean site_admin) {
this.site_admin = site_admin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getCompany() {
return company;
}
public void setCompany(Object company) {
this.company = company;
}
public String getBlog() {
return blog;
}
public void setBlog(String blog) {
this.blog = blog;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Object getEmail() {
return email;
}
public void setEmail(Object email) {
this.email = email;
}
public boolean isHireable() {
return hireable;
}
public void setHireable(boolean hireable) {
this.hireable = hireable;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
public int getPublic_repos() {
return public_repos;
}
public void setPublic_repos(int public_repos) {
this.public_repos = public_repos;
}
public int getPublic_gists() {
return public_gists;
}
public void setPublic_gists(int public_gists) {
this.public_gists = public_gists;
}
public int getFollowers() {
return followers;
}
public void setFollowers(int followers) {
this.followers = followers;
}
public int getFollowing() {
return following;
}
public void setFollowing(int following) {
this.following = following;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
package com.bwei.retofit.bean;
import java.util.List;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2018/3/12 19:12
*/
public class User {
private List<?> ads;
private List<List<String>> dropdown;
public List<?> getAds() {
return ads;
}
public void setAds(List<?> ads) {
this.ads = ads;
}
public List<List<String>> getDropdown() {
return dropdown;
}
public void setDropdown(List<List<String>> dropdown) {
this.dropdown = dropdown;
}
}