一个基于 OkHttp 的 RESTful API 请求工具
Retrofit 在使用时其实就充当了一个适配器(Adapter)的角色,主要是将一个 Java 接口翻译成一个 HTTP 请求对象,然后用 OkHttp 去发送这个请求
核心思想:动态代理—通俗来讲,就是你要执行某个操作的前后需要增加一些操作,比如查看用户个人信息前需要判断用户是否登录,用户访问数据库后想清除用户的访问记录等操作
添加依赖:
implementation ‘com.squareup.retrofit2:retrofit:2.2.0’
implementation ‘com.squareup.retrofit2:converter-gson:2.2.0’
1
2
添加网络权限
1
需要调用的接口:
package com.example.workday12;
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
public interface NetDataInterFace {
//http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
@GET(“ios/cf/{path}”)
Call getData(@Path(“path”)String path, @QueryMap Map<String,String> map);
@FormUrlEncoded
@POST("ios/cf/{path}")
Call<JavaBean> getDataByPOST(@Path("path")String path, @Field("stage_id")String stage_id,@Field("limit")String limit,@Field("page")String page);
}
// An highlighted block
/**
* get请求
*/
private void HttpToGet() {
//http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
Map<String,String> map = new HashMap<>();
map.put("stage_id","1");
map.put("limit","20");
map.put("page","1");
new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(NetDataInterFace.class)
.getData("dish_list.php",map)
.enqueue(new Callback<JavaBean>() {
@Override
public void onResponse(Call<JavaBean> call, Response<JavaBean> response) {
JavaBean body = response.body();
for (int i = 0; i < body.getData().size();i++){
String s = body.getData().get(i).toString();
Log.e("###",s);
}
}
@Override
public void onFailure(Call<JavaBean> call, Throwable t) {
}
});
}
// An highlighted block
post方法获取字符串:
/**
* post请求
*/
private void HttpToPost() {
new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(NetDataInterFace.class)
.getDataByPOST("dish_list.php","1","20","1")
.enqueue(new Callback<JavaBean>() {
@Override
public void onResponse(Call<JavaBean> call, Response<JavaBean> response) {
JavaBean body = response.body();
for (int i = 0; i < body.getData().size();i++){
String s = body.getData().get(i).toString();
Log.e("###",s);
}
}
@Override
public void onFailure(Call<JavaBean> call, Throwable t) {
}
});
}
// An highlighted block
package com.example.day11_fresco;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button fresco;
private Button butter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
fresco = findViewById(R.id.main_fresco);
butter = findViewById(R.id.main_butterknife);
fresco.setOnClickListener(this);
butter.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
switch (v.getId()){
case R.id.main_fresco:
intent.setClass(this,FrescoActivity.class);
break;
case R.id.main_butterknife:
intent.setClass(this,ButterknifeActivity.class);
break;
}
startActivity(intent);
}
}