zero

Zero point one

Retrofit is a popular Http framework for Android. You can set up Http requests, parse JSon, and so on by specifying what resources to request.

What do we want to gain

We are going to get the parsed JSON data at the URL:

https://api.github.com/users/Guolei1130

So let’s say we need to know the JSon login.

Two, the preparation before use

The idea is very clear: 1. Connect the server to get the data. 2. Before using Retrofit we first import Retrofit to add dependencies to build.gradle.

Here — — — — — >

Add this —->

The compile 'com. Squareup. Retrofit2: retrofit: 2.0.0 - beta4' compile 'com. Squareup. Retrofit2: converter - gson: 2.0.0 - beta4'Copy the code

Android Studio will prompt you to synchronize your project. After the synchronization is complete, please apply for network permissions in the androidmanifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />
Copy the code

So it’s ready to use.

B

Parsing class

First we prepare a parser class that specifies the object model we want to fetch:

Code appears, Dadada

public class DataModel{ private String login; Public String getLogin() {return login; }}Copy the code

Interface for parsing

Then we need to define an interface

Public interface RequestServices {@get ("Guolei1130"); // This method returns the requested data in the form of our parse class}Copy the code

We’re using GET to request data. There are many types of annotations, but for now we’ll just use the basics of GET.

Sometimes we request urls with parameters, such as http://www.abc.com/a/b?pram=c, where the URL,? The last one is the parameter, and in this case we’ll do it this way

@GET("b")
Call<DataModel> getData(
    @Query("pram") String c);
Copy the code

The @query annotation is an annotation parameter that is passed when the method is called.

Used in the MainActivity

Without further ado, direct code will tell

Retrofit Retrofit = new Retrofit. The Builder (). The baseUrl (" https://api.github.com/users/ ") / / note that the URL "/" is important not to lose finally AddConverterFactory (GsonConverterFactory. The create ()) / / set data parser, using GSON parsing. The build (); RequestServices = retrofit.create(RequestServices.class); Call<DataModel> Call = requestServices.getList(); Call.enqueue (new Callback<DataModel>() {@override public void onResponse(call <DataModel> call, If (response.issuccess ()){try {DataModel user = response.body(); Log.v("response-------->", user.getlogin ()); } catch (Exception e){ e.printStackTrace(); }} @override public void onFailure(Call<DataModel> Call, Throwable t) {Copy the code

This allows the data to be requested and parsed.

Wait, I haven’t talked about urls yet

For example, if we request http://www.abc.com/a/b, is there a difference between the different baseURL and get annotations? (Of course there is a difference.)

baseUrl Get the URL in the annotation The URL of the final request
http://www.abc.com/a/ /b http://www.abc.com/b
http://www.abc.com/a/ b http://www.abc.com/a/b
http://www.abc.com/a/ http://www.qwe.com/a http://www.qwe.com/a

Therefore, the second method is recommended, which is simple and clear.

So we can request the data we want and grow old together. O is a dynamic programming K

// As a beginner in Android development, if I have any mistakes or shortcomings, please point them out.