Added in Build.gradle (Module:app)
dependencies { ... Implementation ' com.squareup.retrofit2:retrofit:2.4.0 ' implementation ' COM.SQUAREUP.RETROFIT2: Converter-scalars:2.4.0 '}
Eyekeyservice.java
PackageCom.example.buish.androiddraft;ImportRetrofit2. Call;ImportRetrofit2.http.GET;ImportRetrofit2.http.POST;ImportRetrofit2.http.Query; Public InterfaceEyekeyservice {@GET ("Face/check/checking") Call<String> Checkfaceget (@Query ("app_id") string appId, @Query ("App_key") string AppKey, @Query ("url") String URL); @POST ("Face/check/checking") Call<String> Checkfacepost (@Query ("app_id") string appId, @Query ("App_key") string AppKey, @Query ("url") String URL); @GET ("Face/match/match_compare") Call<String> Comparefaceget (@Query ("app_id") string appId, @Query ("App_key") string AppKey, @Query ("Face_id1") String faceId1, @Query ("Face_id2") String faceId2);}
Mainactivity.java
PackageCom.example.buish.androiddraft;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;Importjava.io.IOException;ImportRetrofit2. Call;ImportRetrofit2. Callback;ImportRetrofit2. Response;ImportRetrofit2. Retrofit;Importretrofit2.converter.scalars.ScalarsConverterFactory; Public classMainactivityextendsappcompatactivity {PrivateRetrofit Retrofit; Private FinalString tag = mainactivity.class. GetName (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findviewbyid (R.id.retrofitgetbutton). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewThread () {@Override Public voidrun () {Super. Run (); Try{Response<String> response = Retrofit.create (Eyekeyservice.class). Checkfaceget ("F89ae61fd63d4a63842277e9144a6bd2", "Af1cd33549c54b27ae24aeb041865da2", "https:// gss0.bdstatic.com/-4o3dsag_xi4khgkpowk1hf6hhy/baike/w%3d268%3bg%3d0/sign=321851cefc246b607b0eb572d3c37d71/ 9345d688d43f879428d347b3d81b0ef41bd53a7a.jpg "). Execute (); LOG.D (Tag,"Get---response.body ():" +response.body ()); } Catch(IOException ex) {LOG.E (tag,"Ex:" +ex); }}}.start (); } }); Findviewbyid (R.id.retrofitpostbutton). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewThread () {@Override Public voidrun () {Super. Run (); Try{Response<String> response = Retrofit.create (Eyekeyservice.class). Checkfacepost ("F89ae61fd63d4a63842277e9144a6bd2", "Af1cd33549c54b27ae24aeb041865da2", "https:// gss0.bdstatic.com/-4o3dsag_xi4khgkpowk1hf6hhy/baike/w%3d268%3bg%3d0/sign=321851cefc246b607b0eb572d3c37d71/ 9345d688d43f879428d347b3d81b0ef41bd53a7a.jpg "). Execute (); LOG.D (Tag,"Post---response.body ():" +response.body ()); } Catch(IOException ex) {LOG.E (tag,"Ex:" +ex); }}}.start (); } }); Findviewbyid (R.id.retrofitget0button). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewThread () {@Override Public voidrun () {Super. Run (); Try{Response<String> response = Retrofit.create (Eyekeyservice.class). Comparefaceget ("F89ae61fd63d4a63842277e9144a6bd2", "Af1cd33549c54b27ae24aeb041865da2", " 1fd63d4a63842277e9144a6b "," 1fd63d4a63842277e9144a6b "). Execute (); LOG.D (Tag,"Get---response.body ():" +response.body ()); } Catch(IOException ex) {LOG.E (tag,"Ex:" +ex); }}}.start (); } }); Findviewbyid (R.id.retrofitget1button). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {retrofit.create (eyekeyservice.class). Comparefaceget ("F89ae61fd63d4a63842277e9144a6bd2", "Af1cd33549c54b27ae24aeb041865da2", " 1fd63d4a63842277e9144a6b "," 1fd63d4a63842277e9144a6b "). Enqueue (NewCallback<string>() {@Override Public voidOnresponse (call<string> call, response<string>response) {LOG.D (tag,"Response.body ():" +response.body ()); } @Override Public voidOnFailure (call<string>Call , Throwable t) {LOG.E (tag,"T:" +t); } }); } }); Retrofit=NewRetrofit.builder (). Addconverterfactory (Scalarsconverterfactory.create ()). BASEURL ("https://api.eyekey.com/"). build (); }}
Android development, using retrofit to send HTTP requests