依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
Code
package org.lwt.rxjavat;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Query;
import java.io.IOException;
public class SimpleRetrofit {
public interface ApiService {
@GET("s")
Call<ResponseBody> fetchBaidu(@Query("wd") String wd,
@Query("rqlang") String lang);
}
public static void main(String[] args) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.baidu.com")
.build();
final ApiService apiService = retrofit.create(ApiService.class);
Call<ResponseBody> cr = apiService.fetchBaidu("aaa", "cn");
cr.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
ResponseBody body = response.body();
try {
System.out.println("response.code() = " + response.code());
System.out.println("response.message() = " + response.message());
System.out.println("body = " + new String(body.bytes()));
System.out.println("response.toString() = " + response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
}