Retrofit is introduced

Type safe HTTP client for Android and Java

Features:

1, good performance, fast processing, simple to use

2. Use REST API design style

OKHttp is used by default to handle network requests, which I think is an enhancement to OKHttp.

Gson parsing is used by default

use

Steps: 1. Guide the package

Implementation ‘com. Squareup. Retrofit2: retrofit: 2.9.0’

2. Code steps:

  1. Define an interface (encapsulating URL addresses and data requests)

RequestServices.java

Put the base addresses in a single class to make calling constant.java easier

  1. Instantiate the Retrofit

  1. Create interface service objects from Retrofit instances

  1. The interface service object invokes the methods in the interface to get the Call object

  1. Call objects perform requests (asynchronous, synchronous)

Full code:

import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import java.io.IOException; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; public class testretrofitActivity extends AppCompatActivity { private Context mContext = this; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_testretrofit); initView(); initRetrofit(); } private void initView(){ textView = (TextView) findViewById(R.id.tv_alipay); } private void initRetrofit(){// Get the Retrofit object, Retrofit = new retrofit.builder ().baseurl (constant.url_base).build(); Log.i("LHD","1"); RequestServices requestServices = retrofit.create(RequestServices.class); Call<ResponseBody> call = requestServices.getString(); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.isSuccessful()){ try { Log.i("LHD",response.body().toString()); Response.body () String result = response.body().string(); // The onResponse method is running on the main thread, the UI thread, so we can update UI textView.settext (result) directly here; } catch (IOException e) { e.printStackTrace(); }} @override public void onFailure(Call<ResponseBody> Call, Throwable t) {log. I ("LHD"," access failed "); }}); } private class RequestServices { public Call<ResponseBody> getString() { return null; }}}Copy the code

annotations

Retrofit uses extensive annotations to simplify requests. Retrofit abstracts OKHTTP requests into Java interfaces, using annotations to configure and describe network request parameters.

1. Request method annotations

Request method annotations instructions
@GET A get request
@POST A post request
@PUT Put request
@DELETE The delete request
@PATCH Patch request, which is a complement to the PUT request and is used to update local resources
@HEAD A head request
@OPTIONS The options request
@HTTP All of the above annotations can be replaced by annotations, which have three attributes: Method, Path, and hasBody