Android開發,使用Retrofit發送HTTP請求

來源:互聯網
上載者:User

標籤:compare   style   pre   enc   click   code   .post   cal   --   

在build.gradle(Module: app)中加入
dependencies {    ...    implementation ‘com.squareup.retrofit2:retrofit:2.4.0‘    implementation ‘com.squareup.retrofit2:converter-scalars:2.4.0‘}
EyeKeyService.java
package com.example.buish.androiddraft;import retrofit2.Call;import retrofit2.http.GET;import retrofit2.http.POST;import retrofit2.http.Query;public interface EyeKeyService {    @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
package com.example.buish.androiddraft;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import java.io.IOException;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;import retrofit2.Retrofit;import retrofit2.converter.scalars.ScalarsConverterFactory;public class MainActivity extends AppCompatActivity {    private Retrofit retrofit;    private final String tag = MainActivity.class.getName();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.retrofitGetButton).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                new Thread() {                    @Override                    public void run() {                        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(new View.OnClickListener() {            @Override            public void onClick(View v) {                new Thread() {                    @Override                    public void run() {                        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(new View.OnClickListener() {            @Override            public void onClick(View v) {                new Thread() {                    @Override                    public void run() {                        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(new View.OnClickListener() {            @Override            public void onClick(View v) {                retrofit.create(EyeKeyService.class).compareFaceGet("f89ae61fd63d4a63842277e9144a6bd2", "af1cd33549c54b27ae24aeb041865da2", "1fd63d4a63842277e9144a6b", "1fd63d4a63842277e9144a6b")                        .enqueue(new Callback<String>() {                            @Override                            public void onResponse(Call<String> call, Response<String> response) {                                Log.d(tag, "response.body(): " + response.body());                            }                            @Override                            public void onFailure(Call<String> call, Throwable t) {                                Log.e(tag, "t: " + t);                            }                        });            }        });        retrofit = new Retrofit.Builder().addConverterFactory(ScalarsConverterFactory.create()).baseUrl("https://api.eyekey.com/").build();    }}

 

Android開發,使用Retrofit發送HTTP請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.