retrofit底层请求也是通过OKHttp的,只是对okHttp的一层封装
对库的依赖
compile 'com.squareup.retrofit2:retrofit:2.1.0+' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
注解说明,可以参考
获取字符串
记得要依赖
compile 'com.squareup.retrofit2:converter-scalars:2.0.0',得到字符串的转换器,如果是用GsonConverterFactory.create() 会解析出错,直接到onFailure()中
{
"name":"huanghuanghuang",
"age":"18",
"book":"blue"
}
"name":"huanghuanghuang",
"age":"18",
"book":"blue"
}
代码如下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)//设置超时时间
.readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
.writeTimeout(10, TimeUnit.SECONDS);//设置写入超时时间
OkHttpClient mOkHttpClient = builder.build();
text = (TextView) findViewById(R.id.text);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.119:9999/")
.addConverterFactory(ScalarsConverterFactory.create())
.client(mOkHttpClient) //可以使用自己定制的okhttpClient
.build();
Log.e("onCreate", Thread.currentThread().getName() + "");
BlueService service = retrofit.create(BlueService.class);
Call<String> call = service.getBody();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
text.setText(response.body());//运行在UI线程中的
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
//定义请求接口
public interface BlueService {
@GET("ad.txt")
Call<String> getBody();
}
//简单的Gson实用
{
"name":"huangyoulin",
"age":"18",
"book":"blue"
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.119:9999/")
.addConverterFactory(GsonConverterFactory.create())
.client(mOkHttpClient)
.build();
public interface BlueService {
@GET("ad.txt")
Call<User> getBody();
}
class User{
private String name;
private String age;
private String book;
public User(String name, String age, String book) {
this.name = name;
this.age = age;
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
简单的gson解析
简单的解析gson数据
比如你的数据是这样子的
{
"name":"huanghuanghuang",
"age":"18",
"book":"blue"
}
"name":"huanghuanghuang",
"age":"18",
"book":"blue"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)//设置超时时间
.readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
.writeTimeout(10, TimeUnit.SECONDS);//设置写入超时时间
OkHttpClient mOkHttpClient = builder.build();
text = (TextView) findViewById(R.id.text);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.119:9999/")
.addConverterFactory(GsonConverterFactory.create())
.client(mOkHttpClient)
.build();
Log.e("onCreate", Thread.currentThread().getName() + "");
BlueService service = retrofit.create(BlueService.class);
Call<User> call = service.getBody();
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
text.setText(response.body().age);//运行在UI线程中的
Log.e("onCreate", Thread.currentThread().getName() + "");
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.e("onFailure", "");
}
});
}
public interface BlueService {
@GET("ad.txt")
Call<User> getBody();
}
class User{
private String name;
private String age;
private String book;
public User(String name, String age, String book) {
this.name = name;
this.age = age;
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}