码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发,使用Retrofit发送HTTP请求

时间:2018-09-04 17:05:42      阅读:543      评论:0      收藏:0      [点我收藏+]

标签: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请求

标签:compare   style   pre   enc   click   code   .post   cal   --   

原文地址:https://www.cnblogs.com/buyishi/p/9584502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!