Use Alibaba fit in Android Studio for network requests

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.