工作中发现我们的代码里面使用的retrofit1.9,在前人搭建好的架构下使用起来真的得心应手。真的要给前人一个大大的赞。
良心,不能只是在填坑的时候喷前人,优秀的代码还是要学习的。
既然Retrofit已经升级到2.0了,那就学习使用下2.0的Retrofit吧。
首先说明一下我们要做些什么。
目标:服务器接口提供数据,移动端请求接收数据,对数据处理
说明:需求涉及到网络请求,最好不要在主线程。
实现:
1:后台接口数据类型:
1、接口地址:http://xxxxxxx GET
2、接口参数:无。
3、返回结果:[{"id":1,"name":"名称"},{"id":1,"name":"名称"}]
2:定义数据对象取名为 Ind
package com.example.root.myapplication.test;
/**
* Created by root on 16-11-28.
*/
public class Ind{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.定义接口
package com.example.root.myapplication.test;
import java.util.ArrayList;
import retrofit.Call;
import retrofit.http.GET;
import rx.Observable;
/**
* Created by root on 16-11-28.
*/
public interface IndApi {
@GET("/ind/api/list")
Call<ArrayList<Ind>> getInds();
}
4.创建service。我们在service里面获取接口数据。首先创建一个baseService把一些通用的方法整理进来,这里简单的一个sendMessage方法。
TestBaseService
package com.example.root.myapplication.test;
import android.app.IntentService;
import android.content.Intent;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
/**
* Created by root on 16-11-28.
*/
public class TestBaseService extends IntentService {
public TestBaseService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
if(intent == null){
return;
}
}
protected void sendMessage(Messenger messenger,Object obj){
if(null != obj){
Message msg = Message.obtain();
msg.obj = obj;
try {
messenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
再定义关于Ind的service
package com.example.root.myapplication.test;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Messenger;
import android.util.Log;
import java.util.ArrayList;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
/**
* Created by root on 16-11-28.
*/
public class IndService extends TestBaseService {
private static final String ACTION_GET_INDS = "get_inds";
private static final String MESSENGER = "MESSENGER";
Retrofit retrofit ;
public IndService() {
super("IndService");
}
@Override
public void onCreate() {
super.onCreate();
String baseUrl = "https://xxxxxxxx.com/"; //这里是我们公司后台接口不方便给出
retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static void startGetInds(Context context,Messenger messenger){
Intent intent = new Intent(context,IndService.class);
intent.setAction(ACTION_GET_INDS);
intent.putExtra(MESSENGER,messenger);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if(null == intent) return;
final String action = intent.getAction();
Bundle bundle = intent.getExtras();
if(ACTION_GET_INDS.equals(action)){
getInds(bundle);
}
}
public void getInds(Bundle bundle){
final Messenger messenger = (Messenger) bundle.get(MESSENGER);
IndApi service = retrofit.create(IndApi.class);
service.getInds().enqueue(new Callback<ArrayList<Ind>>() {
@Override
public void onResponse(Response<ArrayList<Ind>> response, Retrofit retrofit) {
if (response.body() != null) {
ArrayList<Ind> inds = response.body();
sendMessage(messenger,inds);
}
}
@Override
public void onFailure(Throwable t) {
sendMessage(messenger,t);
}
});
}
}
这样就可以获取到接口数据了。在需要使用的地方调用IndService.startGetInds()就可以了。
示例:
package com.example.root.myapplication;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.root.myapplication.test.Ind;
import com.example.root.myapplication.test.IndService;
import java.util.ArrayList;
/**
* Created by root on 16-11-28.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInds();
}
private void getInds() {
IndService.startGetInds(MainActivity.this, new Messenger(mHandler));
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i("mzy", "i handle the message");
ArrayList<Ind> ins = (ArrayList<Ind>) msg.obj;
for (Ind i : ins) {
Log.i("mzy", "inds == " + i.getName());
}
}
};
}
就此服务器的数据就加载到了移动端了。数据任你摆布。