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 ());}});