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

Posted buyishi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发,使用Retrofit发送HTTP请求相关的知识,希望对你有一定的参考价值。

在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请求的主要内容,如果未能解决你的问题,请参考以下文章

Android开发学习之Retrofit框架

Android开发学习之Retrofit框架

使用 Retrofit 从 Android 向服务器发送字符串数组

如何在 Android 中使用 Retrofit 发送 JSON POST 请求,并接收字符串响应

【Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...

无法在Android Retrofit中为我的班级创建转换器