1.导入相关相关库
implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
2.书写一个接口
public interface MessageServices {
@GET("GetMessageData")
Call<ResponseCls<MessageInfo>> GetMessageData(@Query("userId") String userId, @Query("password") String password);
}
3.实体类
public class ResponseCls<T> {
private boolean status;
private Integer code;
private List<String> messages = new ArrayList();
private T data;
public ResponseCls() {
}
public int getCode(){
return this.code;
}
public T getData() {
return this.data;
}
}
3.创建retrofit对象
Retrofit retrofit =null
//创建对象
if ( retrofit == null ){
retrofit =new Retrofit.Builder().baseUrl( "http://172.17.0.172:49842/api/ZirukPush/" ).addConverterFactory( GsonConverterFactory.create()).build();
}
4.调用并返回对象
if ( retrofit!=null){
//获取MessageServices对象
final MessageServices messageServices = retrofit.create( MessageServices.class );
Call <ResponseCls<MessageInfo>> call = messageServices.GetMessageData("test","test" );
call.enqueue( new Callback<ResponseCls<MessageInfo>>( ) {
@Override
public void onResponse(Call <ResponseCls<MessageInfo>> call, Response<ResponseCls<MessageInfo>> response) {
if (response != null && response.body() != null && response.body().getData() != null) {
// Toast.makeText(getApplicationContext(), response.body().getData().Content, Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendNotification(response.body().getData().Title,response.body().getData().Content);
}
ZirukPushPlugins.sendEvent( "onReceiveMessageData","模仿页面跳转" );
}
}
@Override
public void onFailure(Call<ResponseCls<MessageInfo>> call, Throwable t) {
//Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
}
} ) ;
}