JDK 7 + Android 2.3 +
The automatic implementation of JSON to JavaBean, is an enhanced version of Okhttp, the underlying packaging Gson framework, but must be a new Gson object
Master notes @GET ("Home") @Query ("index") @FormUrlEncoded @Field ("username"), etc.
Your own way of thinking steps:
1, create the retrofit object, and set the parameters of the retrofit and convert the Gson
2, extracting the network request method required by the project Get () login (), etc., into the interface API, retrofit interface processing
3,retrofit processing interface (including methods in the interface) to obtain the requested object
4, write callback object logic, callback
Specific implementation of Demo:
Compile ' com.squareup.retrofit2:retrofit:2.1.0 ' com.squareup.retrofit2:converter-gson:2.1.0 ' JSON-to-javabean conversion
Configure permissions
<uses-permission android:name= "Android.permission.INTERNET"/>
MyApi interface
Public InterfaceGooglemarketapi {//http://127.0.0.1: 8090/home?index=0//annotation @get: Specifying the method and request path//@Query is used to set the GET request parameter variable name//The return value generic is used to specify new Gson (). Fromjson (JSON,CLZ)//Specifies that call the method can run a child thread, representing an asynchronous request@GET ("Home") Call<HomeData>Gethomedata (@Query ("Index"), String index); //@QueryMap specifying the map collection as a GET request parameter@GET ("Home") Call<HomeData>gethomedata (@QueryMap HashMap<String,String>map); //http://192.168.79.28: 8080/webapi/post?username=itheima&password=123//@POST Labeling The current method using a POST request//@Field used to label the name of the POST request parameter//@FormUrlEncoded used in conjunction with @post, requires network encoding of post parameters@FormUrlEncoded @POST ("POST") Call<LoginData> Login (@Field ("username") String username, @Field ("Password") String pwd); //@FieldMap The parameters used to label the map as a POST request@FormUrlEncoded @POST ("POST") Call<LoginData> Login (@FieldMap hashmap<string,string>params);}
Methods in the Mainactivity.java
Public voidrequest (view view) {//Step two. Start sending requests using retrofit//2.1. Initialize the framework Alertdialog.builder Settitle and Retrofit.builder compare the former to quickly create a dialog which quickly initializes the Retrofit frameString baseurl= "http://192.168.79.28:8080/webapi/";//retrofit require/end, otherwise an error will be generatedRetrofit Retrofit =NewRetrofit.builder (). BASEURL (BASEURL)//Set project path other paths are changed to relative paths with Apiurls HOST home=host+ "/home"/. Addconverterfactory (Gsonconverterfactory.create (NewGson ()))//convert is to parse JSON into JavaBean. build ();//Execute Creation Method//2.2. Call the retrofit framework to read all the parameters required for a request, which are annotated or genericGooglemarketapi Googlemarketapi = Retrofit.create (Googlemarketapi.class);//Read Request method request page return parameters//2.3. You can get an async method//Call<logindata> method = Googlemarketapi.login ("Itheima", "123");Hashmap<string, string> params=NewHashmap<>(); Params.put ("username", "Itheima"); Params.put ("Password", "123"); Pager<LoginData> method =Googlemarketapi.login (params); //2.4. Executing a sub-thread, processing the service-side return data is to pass the data to the callback object (empty method, write business logic to the developer, pay attention to the condition)Callback<logindata> callback=NewCallback<logindata>() { //2.4.1. Business logic to handle request success@Override Public voidOnresponse (call<logindata> call, response<logindata>response) { //2.4.2. Get the parsed JavaBean from the responseLogindata data =Response.body (); if(Data! =NULL) {textview.settext (data.data); } } //2.4.3. Business logic to handle request failures@Override Public voidOnFailure (call<logindata>Call , Throwable t) {T.printstacktrace (); Textview.settext (T.getmessage ()); } }; Method.enqueue (callback);}
Retrofit Network Framework