Retrofit 注解的使用

package com.baway.alex.retrofitdemo.di;

public class Constant {
    //存放完整地址
    public static final String URL = "http://112.124.22.238:8081/course_api/cniaoplay/featured?p={%22page%22:0}";
    // 存放完整POST请求地址
    public static final String POSTURL = "http://101.200.41.116:8099/UserQry";
    //存放的完整get请求的替换接口
    public static final String REPLACEURL = "https://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1";

    //存放的根地址
    public static final String BASE_URL = "http://112.124.22.238:8081/course_api/cniaoplay/";
    //存放的POST根地址
    public static final String POST_BASE_URL = "http://101.200.41.116:8099/";

    //查询的条件
    public static final String REQUESTMSG = "featured";
    public static final String REQUESTKEY = "p";
    public static final String REQUESTVALUE = "{\"page\":0}";
}

package com.baway.alex.retrofitdemo.ui.activity;

 

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

import com.baway.alex.retrofitdemo.R;

import com.baway.alex.retrofitdemo.data.beans.FormatBean;

import com.baway.alex.retrofitdemo.di.Api;

import com.baway.alex.retrofitdemo.di.Constant;

 

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

 

import butterknife.BindView;

import butterknife.ButterKnife;

import butterknife.OnClick;

import okhttp3.ResponseBody;

import retrofit2.Call;

import retrofit2.Callback;

import retrofit2.Response;

import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

 

public class MainActivity extends AppCompatActivity {

 

@BindView(R.id.btn_getRequest)

Button btnGetRequest;

@BindView(R.id.btn_getRequestByQuery)

Button btnGetRequestByQuery;

@BindView(R.id.btn_getRequestByQueryMap)

Button btnGetRequestByQueryMap;

@BindView(R.id.btn_getRequestByPath)

Button btnGetRequestByPath;

@BindView(R.id.btn_getHttpRequest)

Button btnGetHttpRequest;

@BindView(R.id.btn_postRequest)

Button btnPostRequest;

@BindView(R.id.btn_postRequestWithFieldMap)

Button btnPostRequestWithFieldMap;

@BindView(R.id.btn_replaceRequest)

Button btnReplaceRequest;

@BindView(R.id.btn_replaceRequestCompare)

Button btnReplaceRequestCompare;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ButterKnife.bind(this);

}

 

@OnClick({R.id.btn_getRequest, R.id.btn_getRequestByQuery, R.id.btn_getRequestByQueryMap, R.id.btn_getRequestByPath, R.id.btn_getHttpRequest, R.id.btn_postRequest, R.id.btn_postRequestWithFieldMap, R.id.btn_replaceRequest,R.id.btn_replaceRequestCompare})

public void onViewClicked(View view) {

switch (view.getId()) {

case R.id.btn_getRequest:

//通过Retrofit进行基本请求

getRequestMsg();

break;

case R.id.btn_getRequestByQuery:

//通过Retrofit的Query注解进行请求

requestMsgByQuery();

break;

case R.id.btn_getRequestByQueryMap:

//通过Retrofit的QueryMap注解进行请求

requestMsgByQueryMap();

break;

case R.id.btn_getRequestByPath:

//通过Retrofit的Path注解进行请求

requestMsgByPath();

break;

case R.id.btn_getHttpRequest:

//通过Http注解进行Get请求

getHttpRequest();

break;

case R.id.btn_postRequest:

//通过Post注解进行Post请求

requestMsgByPost();

break;

case R.id.btn_postRequestWithFieldMap:

通过Post注解结合FieldMap注解进行Post请求

requestMsgByPostWithFieldMap();

break;

case R.id.btn_replaceRequest:

//通过url注解进行替换

requestByGetWithReplace();

break;

case R.id.btn_replaceRequestCompare:

replaceRequestCompare();

break;

}

}

 

private void replaceRequestCompare() {

Retrofit retrofit = new Retrofit.Builder()

.baseUrl(Constant.BASE_URL)

.addConverterFactory(GsonConverterFactory.create())

.build();

Api api = retrofit.create(Api.class);

Call<FormatBean> call = api.getCompareResponseMsg(Constant.URL);

call.enqueue(new Callback<FormatBean>() {

@Override

public void onResponse(Call<FormatBean> call, Response<FormatBean> response) {

String versionName = response.body().getDatas().get(0).getVersionName();

Toast.makeText(MainActivity.this, versionName, Toast.LENGTH_SHORT).show();

}

 

@Override

public void onFailure(Call<FormatBean> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void requestByGetWithReplace() {

Retrofit retrofit = new Retrofit.Builder()

.baseUrl(Constant.BASE_URL)

.build();

Api api = retrofit.create(Api.class);

Call<ResponseBody> call = api.getReplaceResponseMsg(Constant.URL);

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

try {

String resposneString = response.body().string();

Toast.makeText(MainActivity.this, resposneString, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void requestMsgByPostWithFieldMap() {

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.POST_BASE_URL).build();

Api api = retrofit.create(Api.class);

HashMap<String, String> map = new HashMap<>();

//token 任意字串

//type 0

//op_code ""

//phone_no 18301154407

map.put("token", "");

map.put("type", "0");

map.put("op_code", "");

map.put("phone_no", "18301154407");

Call<ResponseBody> call = api.getResponseMsgWithPostThrowFieldMap(map);

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

try {

String resposneString = response.body().string();

Toast.makeText(MainActivity.this, resposneString, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void requestMsgByPost() {

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.POST_BASE_URL).build();

Api api = retrofit.create(Api.class);

//token 任意字串

//type 0

//op_code ""

//phone_no 18301154407

Call<ResponseBody> call = api.getResponseMsgWithPost("", "0", "", "18301154407");

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

try {

String resposneString = response.body().string();

Toast.makeText(MainActivity.this, resposneString, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void getHttpRequest() {

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).build();

//采用Http注解去处理

retrofit.create(Api.class).httpGetResponseMsg().enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

String responseMsg = null;

try {

responseMsg = response.body().string();

Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void requestMsgByPath() {

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).build();

Api api = retrofit.create(Api.class);

//参数添加

Map<String, String> map = new HashMap<>();

map.put(Constant.REQUESTKEY, Constant.REQUESTVALUE);

//所需参数,都提交完毕再进行请求

Call<ResponseBody> call = api.getResponseMsgByPath(Constant.REQUESTMSG, map);

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

String responseMsg = null;

try {

responseMsg = response.body().string();

Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void requestMsgByQueryMap() {

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).build();

Api api = retrofit.create(Api.class);

//传递所需的参数

Map<String, String> map = new HashMap<>();

map.put("p", "{\"page\":0}");

Call<ResponseBody> call = api.getResponseMsgByQueryMap(map);

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

String responseMsg = null;

try {

responseMsg = response.body().string();

Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

 

private void requestMsgByQuery() {

//通过Query注解进行请求

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).build();

//创建接口的实例化对象

Api api = retrofit.create(Api.class);

Call<ResponseBody> call = api.getResponseMsgByQuery("{\"page\":1}");

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

try {

String reponseMsg = response.body().string();

Toast.makeText(MainActivity.this, reponseMsg, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String responseMsg = t.getMessage().toString();

Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_SHORT).show();

}

});

}

 

private void getRequestMsg() {

//实例化Retrofit对象

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).build();

//通过Retrofit实例创建接口服务对象

Api api = retrofit.create(Api.class);

//接口服务对象调用接口中的方法,获取Call对象

Call<ResponseBody> call = api.getResponseMsg();

//Call对象执行请求(异步、同步请求)

call.enqueue(new Callback<ResponseBody>() {

@Override

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

String responseString = null;

try {

responseString = response.body().string();

Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

}

}

 

@Override

public void onFailure(Call<ResponseBody> call, Throwable t) {

String errorString = t.getMessage().toString();

Toast.makeText(MainActivity.this, errorString, Toast.LENGTH_SHORT).show();

}

});

}

 

 

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值