Current version: 2.7.1
Official document: https://square.github.io/retrofit/
Retrofit It is a RESTful encapsulation of HTTP network request framework.
brief introduction
Use
- Add a dependency on the Retrofit Library
- rely on
dependencies { ... implementation 'com.squareup.retrofit2:retrofit:2.7.1' implementation 'com.squareup.retrofit2:converter-gson:2.3.0' }
- Jurisdiction
<uses-permission android:name="android.permission.INTERNET"/>
- Create a class to receive data from the server
Translation.java
/** * created on 2020/2/11 10:18 * * @author Scarf Gong */ public class Translation { /** * status : 1 * content : {"from":"en-EU","to":"zh-CN","vendor":"wps","out":"Hello world, Ciba Gu use: from machine translation. ","ciba_out":"","err_no":0} */ private int status; private ContentBean content; public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public ContentBean getContent() { return content; } public void setContent(ContentBean content) { this.content = content; } public static class ContentBean { /** * from : en-EU * to : zh-CN * vendor : wps * out : Hello world * ciba_use : From machine translation. * ciba_out : * err_no : 0 */ private String from; private String to; private String vendor; private String out; private String ciba_use; private String ciba_out; private int err_no; public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getVendor() { return vendor; } public void setVendor(String vendor) { this.vendor = vendor; } public String getOut() { return out; } public void setOut(String out) { this.out = out; } public String getCiba_use() { return ciba_use; } public void setCiba_use(String ciba_use) { this.ciba_use = ciba_use; } public String getCiba_out() { return ciba_out; } public void setCiba_out(String ciba_out) { this.ciba_out = ciba_out; } public int getErr_no() { return err_no; } public void setErr_no(int err_no) { this.err_no = err_no; } @Override public String toString() { return "ContentBean{" + "from='" + from + '\'' + ", to='" + to + '\'' + ", vendor='" + vendor + '\'' + ", out='" + out + '\'' + ", ciba_use='" + ciba_use + '\'' + ", ciba_out='" + ciba_out + '\'' + ", err_no=" + err_no + '}'; } } }
- Create an interface to describe network requests
/** * created on 2020/2/11 10:22 * * @author Scarf Gong */ public interface IRequest { @GET("ajax.php?a=fy&f=auto&t=auto&w=hello%20world") Call<Translation> getCall(); }
- Create Retrofit instance
- Create a network request interface instance and configure network request parameters
- Send network request (asynchronous / synchronous)
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); request(); } private void request() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://fy.iciba.com/") // Set network request Url .addConverterFactory(GsonConverterFactory.create()) //Set to use Gson resolution (remember to add dependency) .build(); IRequest request = retrofit.create(IRequest.class); //Encapsulate send request Call<Translation> call = request.getCall(); call.enqueue(new Callback<Translation>() { //Callback on successful request @Override public void onResponse(Call<Translation> call, Response<Translation> response) { //Processing returned data results int status = response.body().getStatus(); Log.d(TAG, "onResponse: " + status); } //Callback on request failure @Override public void onFailure(Call<Translation> call, Throwable throwable) { Log.d(TAG, "connection failed"); } }); } }
Contrast
In addition to Retrofit, the mainstream network request frameworks in Android today are:
- Android-Async-Http
- Volley
- OkHttp
226 original articles published, 66 praised, 210000 visitors+