1.简介
Retrofit是 Square 公司开源的适用于Android与Java的网络请求库,是对其先前开源的OKHTTP的进一步封装。
官网上的说这是一个为Android和Java打造的类型安全的HTTP客户端
2.一般客户端与服务器如何进行交互
android中客户端与服务器的交互和浏览器与客户端交互差不多
HTTP请求报文
HTTP响应报文
3.如何使用retrofit
- 示意图:
- 需要添加的依赖
//用于转换数据
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
//butter knife
compile 'com.jakewharton:butterknife:8.4.0'
compile 'com.jakewharton:butterknife-compiler:8.4.0'
- 服务器API要求
- IPService
package com.example.geekp.ipinforetrofit.ip;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;
/**
* Created by geekp on 2017/1/2.
* email:810275469@qq.com
*/
public interface IPService {
@GET("/apistore/iplookupservice/iplookup")
Call<IPCallback> getCallback(@Header("apikey") String apikey, @Query("ip") String ip);
}
- IPAPI
package com.example.geekp.ipinforetrofit.ip;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by geekp on 2017/1/2.
* email:810275469@qq.com
*/
public class IPAPI {
public static final String BASEURL = "http://apis.baidu.com";
public static final String APIKEY = "1ec3be7a7397690b51447990ab8276b2";
private static IPAPI INSTANCE = null;
private IPService service;
private IPAPI() {
//创建retrofit对象
Retrofit retrofit = new Retrofit
.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASEURL)
.build();
//创建访问API的请求
service = retrofit.create(IPService.class);
}
//lazy singleton doublecheck
public static IPAPI getINSTANCE() {
if (null == INSTANCE) {
synchronized (IPAPI.class) {
if (INSTANCE == null) {
INSTANCE = new IPAPI();
}
}
}
return INSTANCE;
}
public IPService getService() {
return service;
}
}
- IPCallback
package com.example.geekp.ipinforetrofit.ip;
/**
* Created by Gpwner on 2017/1/2 9:34
* Email:810275469@qq.com
*/
public class IPCallback {
private int errNum;
private String errMsg;
private EntityData retData;
public int getErrNum() {
return errNum;
}
public void setErrNum(int errNum) {
this.errNum = errNum;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public EntityData getRetData() {
return retData;
}
public void setRetData(EntityData retData) {
this.retData = retData;
}
public static class EntityData{
private String ip;
private String country;
private String province;
private String city;
private String district;
private String carrier;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getCarrier() {
return carrier;
}
public void setCarrier(String carrier) {
this.carrier = carrier;
}
}
}
- Mainactivity
package com.example.geekp.ipinforetrofit;
/**
* Created by Gpwner on 2017/1/2 16:55
* Email:810275469@qq.com
*/
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.geekp.ipinforetrofit.ip.IPAPI;
import com.example.geekp.ipinforetrofit.ip.IPCallback;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.editsearch)
EditText editsearch;
@BindView(R.id.country)
TextView country;
@BindView(R.id.province)
TextView province;
@BindView(R.id.city)
TextView city;
@BindView(R.id.district)
TextView district;
@BindView(R.id.carrier)
TextView carrier;
@BindView(R.id.btnsearch)
Button btnsearch;
private IPAPI ipapi = IPAPI.getINSTANCE();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btnsearch)
public void onClick() {
String ip = editsearch.getText().toString();
if (ip == "") {
return;
}
//接收返回服务器响应结果
Call<IPCallback> call = ipapi.getService().getCallback(IPAPI.APIKEY, ip);
call.enqueue(new Callback<IPCallback>() {
@Override
public void onResponse(Call<IPCallback> call, Response<IPCallback> response) {
if (response.isSuccessful()) {
IPCallback ipCallback = response.body();
if (ipCallback != null && ipCallback.getErrNum() == 0) {
IPCallback.EntityData data = ipCallback.getRetData();
if (data != null) {
country.setText(data.getCountry());
province.setText(data.getProvince());
city.setText(data.getCity());
district.setText(data.getDistrict());
carrier.setText(data.getCarrier());
}
}
}
}
@Override
public void onFailure(Call<IPCallback> call, Throwable t) {
System.out.println("请求失败");
}
});
}
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的IP" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="80dp"
android:text="国家:"
android:textSize="24sp" />
<TextView
android:id="@+id/country"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="80dp"
android:text="省份:"
android:textSize="24sp" />
<TextView
android:id="@+id/province"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="80dp"
android:text="城市:"
android:textSize="24sp" />
<TextView
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:text="地区:"
android:layout_height="40dp"
android:layout_marginLeft="80dp"
android:textSize="24sp" />
<TextView
android:id="@+id/district"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="80dp"
android:text="运营商:"
android:textSize="24sp" />
<TextView
android:id="@+id/carrier"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<Button
android:id="@+id/btnsearch"
android:layout_marginLeft="100dp"
android:text="查询"
android:textSize="24sp"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>