如何在 android studio 中使用 Retrofit?我的问题是什么?

How to use Retrofit in android studio? and what is my problem?

我在“TestService.java”中做了这样的界面

  public interface TestService {
        @GET("/api/users/2")
        Call<String> getTest();
    }

"RetrofitClient.java"

public class RetrofitClient {
    private  static Retrofit instance;

    public static Retrofit getInstance() {
        if(instance == null)
            instance = new Retrofit.Builder()
                    .baseUrl("https://reqres.in/")
                    .build();
        return instance;
    }
}

在“MainActivity.java”

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofitClient = RetrofitClient.getInstance();
        TestService testService =  retrofitClient.create(TestService.class);
        Call<String> repos = testService.getTest(); //problem

    }
}

我是第一个在 android java,我不知道如何使用 Retrofit.. 问题是什么以及如何打印响应? (我还需要 header 信息)

需要在主线程异步调用

    Call<ResponseBody> repos = testService.getTest();

    repos.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
           //response
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
           //error
        }
    });

网上资源很多,想了解更多可以看下面的文章。

vogella

android.jlelse