1.代码
IGetRequest
public interface IGetRequest {
@GET("news/v2/sunweihao/269")//这里要自己可请求的url,这个不可用
Call<Translation> getCall();
}
Translation,这就是bean类,可以配合Gson来进行解析
//缩略,自己得到json文件后,再来转bean
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNetWork(View view) {
request();
// 使用Retrofit封装的方法
}
public void request() {
//步骤4:创建Retrofit对象,包含域名(头部url),gson解析
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.sunweihao.com/") // 设置 网络请求 Url
.addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
.build();
// 步骤5:创建 网络请求接口 的实例
IGetRequest request = retrofit.create(IGetRequest.class);
//对 发送请求 进行封装
Call<Translation> call = request.getCall();
//步骤6:发送网络请求(异步)
call.enqueue(new Callback<Translation>() {
//请求成功时回调
@Override
public void onResponse(Call<Translation> call, Response<Translation> response) {
// 步骤7:处理返回的数据结果
Log.d(TAG, "onResponse: "+response.body().toString());
}
//请求失败时回调
@Override
public void onFailure(Call<Translation> call, Throwable throwable) {
System.out.println("连接失败");
}
});
}
private static final String TAG = "MainActivity";
}
2.讲解与步骤
1.创建创建Retrofit对象,包含域名(头部url),gson解析
//步骤4:创建Retrofit对象,包含域名(头部url),gson解析
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://m.cryptouat.com/") // 设置 网络请求 Url
.addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
.build();
2.编写IGetRequest类,这个类重要用来拼接链接尾部
/**
* 创建日期:2022/2/10 16:10
*
* @author tony.sun
* 类说明:这里只有一个get,主要用来拼接url后面的地址
*/
public interface IGetRequest {
@GET("news/v2/more/269")
Call<Translation> getCall();
}
3.通过retrofit来创建我们写注解@get的类IGetRequest,这里会拼接链接,拼接成完整的链接。然后获取电话来打电话。
// 步骤5:创建 网络请求接口 的实例
IGetRequest request = retrofit.create(IGetRequest.class);
//对 发送请求 进行封装
Call<Translation> call = request.getCall();
4.根据泛型来网络请求,这里的泛型是Translation
//步骤6:发送网络请求(异步)
call.enqueue(new Callback<Translation>() {
//请求成功时回调
@Override
public void onResponse(Call<Translation> call, Response<Translation> response) {
// 步骤7:处理返回的数据结果
Log.d(TAG, "onResponse: "+response.body().toString());
}
//请求失败时回调
@Override
public void onFailure(Call<Translation> call, Throwable throwable) {
System.out.println("连接失败");
}
});
5.记得加权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.global.retrofit">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:usesCleartextTraffic="true"
android:allowClearUserData="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Retrofit">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>