A type-safe HTTP client for Android and Java
http://square.github.io/retrofit/
因为其简单与出色的性能,Retrofit 是安卓上最流行的HTTP Client库之一,配合Rxjava使用效果更佳
这里以一个Demo的形式来记录Retrofit的简单使用
示例使用和风天气的api获取城市列表
https://api.heweather.com/x3/citylist?search=allchina&key=“您的api key”
来获取国内所有的城市列表
每日免费3000次查询也足够个人开发者了
在build.gradle中添加
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
根据json返回的数据建立CityEntity.java
使用Android Studio的下载个GsonFormat直接生成javabean简单又快捷
由于代码量大所以就不贴上来了
activity_main.xml代码如下所示,就是一个listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
创建一个接口CityService.java
public interface CityService {
@GET("x3/citylist")//对应后缀,这里用的是GET方法获取
Call<CityEntity> getCity(@Query("search")String getType,@Query("key")String apiKey);
//这里对应的是主页后缀x3/citylist?=allchina&key="您的api key"
//String getType 对应的是search=
//String apiKey 对应的是key=
//假如没有任何附加参数的网址可以直接这样写
//@GET()
//Call<bean> get();
}
使用Retrofit
private void getCity() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.heweather.com/")//传入URL
.addConverterFactory(GsonConverterFactory.create())//加入gson解析
.build();
CityService cityService = retrofit.create(CityService.class);
Call<CityEntity> call = cityService.getCity("allchina", "您的apiKey");
call.enqueue(new Callback<CityEntity>() {
//成功时执行
@Override
public void onResponse(Call<CityEntity> call, Response<CityEntity> response) {
//所执行的操作
}
//失败时执行
@Override
public void onFailure(Call<CityEntity> call, Throwable t) {
//所执行的操作
}
});
}
MainActivity.java代码如下所示
package com.lzokks04.retrofitdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
private ListView mListView;
private ArrayAdapter<String> adapter;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listview);
getCity();
}
private void getCity() {
String baseURL = "https://api.heweather.com/";
String cityType = "allchina";
String apiKey = "您的APIKEY";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
CityService cityService = retrofit.create(CityService.class);
Call<CityEntity> call = cityService.getCity(cityType, apiKey);
call.enqueue(new Callback<CityEntity>() {
@Override
public void onResponse(Call<CityEntity> call, Response<CityEntity> response) {
list = getCityList(response.body());
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
mListView.setAdapter(adapter);
}
@Override
public void onFailure(Call<CityEntity> call, Throwable t) {
Log.i(TAG, t.getMessage());
}
});
}
/**
* 提取javabean中的城市信息
* @param bean
* @return
*/
private List<String> getCityList(CityEntity bean) {
List<String> cityList = new ArrayList<String>();
for (int i = 0; i < bean.getCity_info().size(); i++) {
cityList.add(bean.getCity_info().get(i).getCity());
}
return cityList;
}
}
最后一定要记住
<uses-permission android:name="android.permission.INTERNET"/>