Use Alibaba fit in Android Studio for network requests
The environment requires that you add references under the GRADLE configuration file for Java 6 and later versions of Android 2.3 and later.
dependencies { ... compile 'com.squareup.retrofit:retrofit:1.9.0' ...}
When OkHttp exists, Retrofit uses OkHttp for network requests. The following is an example of adding a reference using OkHttp:
dependencies { ...compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'compile 'com.squareup.okhttp:okhttp:2.0.0' ...}
Initiate a request
The following example uses the asynchronous fit function to perform a simple asynchronous GET request. The returned value is of the String type.
First, define an interface and declare a method in the interface to define the relevant content of the request.
interface User { @GET("/user/list.json") void getUsers(@Query("pagesize") int pagesize, Callback
callback); }
@ GET indicates that the request is in the GET mode. In addition, there are @ POST, @ PUT, @ DELETE, and @ HEAD. For details, refer to the official documentation, this is not described here.
@ Query: parameter Declaration
Callback <String> is the Callback interface. String indicates that the returned data is of the String type.
Create a RestAdapter object. The Code is as follows:
RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(CTX).setConverter(new BaseConverter()) .build();
The setEndpoint method specifies the First Half of the request address, that is, the server address, as shown in figureHttps://api.github.com
The setConverter method requires an implementation class of the conversion interface as a parameter. The function of this class is to convert the InputStream obtained by the request to the desired type. The sample code is as follows:
public class BaseConverter implements Converter { @Override public Object fromBody(TypedInput body, Type type) throws ConversionException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; String response = ""; try { while ((i = body.in().read()) != -1) { baos.write(i); } response = baos.toString(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return response; } @Override public TypedOutput toBody(final Object object) { return new TypedOutput() { @Override public String fileName() { return null; } @Override public String mimeType() { return "String"; } @Override public long length() { return object == null ? 0 : object.toString().length(); } @Override public void writeTo(OutputStream out) throws IOException { out.write(object.toString().getBytes()); } }; }}
After a restAdapter object is obtained, call the create method of restAdapter to obtain the implementation class of the User interface.
Callback
Callback = new Callback
() {@ Override public void success (String s, Response response) {}@ Override public void failure (inclufiterror error) {}} User user User = getAdapter (). create (User. class); // specifies the Request Parameters and callback interface implementation class user. getUsers (12, callback );
This is the power of Retrofit. It obfuscation the interface code based on the method annotation of the interface.
If you need code obfuscation, configure the following:
-dontwarn retrofit.**-keep class retrofit.** { *; }-keepattributes Signature-keepattributes Exceptions