Simple ingress fit2.0 request network and ingress fit2.0 request

Source: Internet
Author: User

Simple ingress fit2.0 request network and ingress fit2.0 request

1. Add dependency

compile 'com.squareup.retrofit2:retrofit:2.0.0'  compile 'com.squareup.retrofit2:converter-gson:2.0.2' 

2. compile tools

Package com. bwie. skn. simulationoneweek. utils; import java. util. concurrent. timeUnit; import okhttp3.OkHttpClient; import using fit2.20.fit; import using fit2.converter. gson. gsonConverterFactory;/*** author: Created by Conan on. */public class extends fitutils {public static final String BASE_URL =" https://result.eolinker.com/ "; // Request Header private final variable fit m1_fit; public static class SINGLE_HOLDER {public static final variable fitutils INSTANCE = new variable fitutils (BASE_URL);} public static variable fitutils getInstance () {return SINGLE_HOLDER.INSTANCE ;} private response fitutils (String baseUrl) {mshortfit = buildRetrofit ();} // custom OkHttpClient (you can leave it empty) private OkHttpClient buildOkHttpClient () {return new OkHttpClient. builder (). connectTimeout (10000, TimeUnit. MILLISECONDS ). build ();} // set private transfer fit buildRetrofit () {return new transfer fit. builder (). client (buildOkHttpClient ()). baseUrl (BASE_URL ). addConverterFactory (GsonConverterFactory. create ()). build ();} // return public data
 
  
T create (Class
  
   
Clazz) {return m1_fit. create (clazz );}}
  
 

3. Compile tool Interfaces
Package com. bwie. skn. simulationoneweek. interfaces; import com. bwie. skn. simulationoneweek. bean. weekBean; import using fit2.call; import using fit2.http. GET; import requests fit2.http. query;/*** author: Created by Conan on. */public interface using fitapi {@ GET ("umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611") // Request body Call
 
  
GetBean (@ Query ("uri") String uri );}
 


Iv. Use of tool classes

Response fitapi response fitapi = Response fitutils. getInstance (). create (Response fitapi. class); // Call The connection interface
 
  
WeekCall = Response fitapi. getBean ("homepage"); // send the request parameter weekCall. enqueue (new Callback
  
   
() {// Request succeeded @ Override public void onResponse (Call
   
    
Call, Response
    
     
Response) {WeekBean body = response. body (); WeekBean. dataBean data = body. getData (); iweekPresenter. onSuccess (data);} // request failed @ Override public void onFailure (Call
     
      
Call, Throwable throwable) {iweekPresenter. onFailed (throwable. getMessage ());}});
     
    
   
  
 

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.