這里okhttp使用了OkHttpUtils误趴,來源于洪洋大神的封裝。
導入方法
__AndroidStudio: __
在build.gradle中引入
compile 'com.zhy:okhttputils:2.6.2'
因為同時要使用okhttp票编,必須還要引入
compile 'com.squareup.okhttp3:okhttp:3.5.0'
__ Eclipse:__
下載jar包
OkHttpUtils給我們提供了十多種類,包括常用的get祖灰、post請求诀豁,和表單提交,上傳下載等功能阱驾,可以滿足我們開發(fā)的基本需求了就谜。
這里寫了一個比較簡單的網(wǎng)絡訪問,請求接口的demo里覆,用的是聚合數(shù)據(jù)的手機號碼歸屬地丧荐。
下面是完成的效果圖。
在聚合中找到數(shù)據(jù)查看接口調(diào)用方法:
phone 就是我們從控件的到的手機號
key 是聚合固定的喧枷,在我們申請數(shù)據(jù)的時候就給生成了虹统。
測試一下返回結(jié)果,用于生成model
JSON返回示例:
{
"resultcode":"200",
"reason":"Return Successd!",
"result":{
"province":"浙江",
"city":"杭州",
"areacode":"0571",
"zip":"310000",
"company":"中國移動",
"card":"移動動感地帶卡"
}
}
然后我們可以用GsonFormat 快速生成model類
public class NewBeans {
/**
* resultcode : 200
* reason : Return Successd!
* result : {"province":"廣東","city":"中山","areacode":"0760","zip":"528400","company":"移動","card":""}
* error_code : 0
*/
private String resultcode;
private String reason;
private ResultBean result;
private int error_code;
public String getResultcode() {
return resultcode;
}
public void setResultcode(String resultcode) {
this.resultcode = resultcode;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public static class ResultBean {
/**
* province : 廣東
* city : 中山
* areacode : 0760
* zip : 528400
* company : 移動
* card :
*/
private String province;
private String city;
private String areacode;
private String zip;
private String company;
private String card;
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 getAreacode() {
return areacode;
}
public void setAreacode(String areacode) {
this.areacode = areacode;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
}
}
Activity
拼接Url地址
private String utl_ok = "http://apis.juhe.cn/mobile/get?phone=";
private String KEY = "&key=6e1ce94fe231e817fb31daec3b3084d0";
private String URL;
......
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httptest);
ButterKnife.bind(this);
URL = utl_ok + etHttp.getText() + KEY;
使用OkhttpUtils進行g(shù)et請求
這里的key也可以用.addParams("Key",KEY)
添加隧甚,.execute
返回請求結(jié)果
得到的結(jié)果存放在model车荔,然后直接用get方法解析展示。
public void okhttpGet() {
OkHttpUtils
.get()
.url(URL)
.addParams("phone", etHttp.getText().toString())
.build()
.execute(new StringCallback() {
@Override
public void onError(okhttp3.Call call, Exception e, int id) {
UToasts.showShort(OkhttpRetrofitActivity.this, "請求失敗:" + e.getMessage().toString());
}
@Override
public void onResponse(String response, int id) {
Gson gson = new Gson();
NewBeans newBeans = gson.fromJson(response, NewBeans.class);
newBeans.getResult();
UToasts.showLong(OkhttpRetrofitActivity.this, newBeans.getReason());
if (newBeans.getResultcode().equals("200")) {
tv.setText(
"势莅狻:" + newBeans.getResult().getProvince().toString()
+ "\n"
+ "市:" + newBeans.getResult().getCity().toString()
+ "\n"
+ "區(qū)號:" + newBeans.getResult().getAreacode()
+ "\n"
+ "運營商:" + newBeans.getResult().getCompany());
} else {
tv.setText("手機號碼輸入有誤忧便,查詢失敗");
}
}
});
}
post方法跟get用法一樣。
源碼都在github上帽借,需要的可以去查看
地址:https://github.com/wapchief/android-CollectionDemo
如果要獲取響應詳情的話珠增,可以用Fiddler抓包查看超歌。