1 Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create()) //请求返回字符串,如需返回对象,需使用converter-gson
.baseUrl("http://www.baidu.com").build();
2 DataService service = retrofit.create(DataService.class);
1 public interface DataService {
2 @GET("/")
3 Call<String> getData();
4 }
Call<String> data = service.getData();
//String msg=data.execute().body().toString();//同步执行
//异步执行
data.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
- Gson:
com.squareup.retrofit2:converter-gson
- Jackson:
com.squareup.retrofit2:converter-jackson
- Moshi:
com.squareup.retrofit2:converter-moshi
- Protobuf:
com.squareup.retrofit2:converter-protobuf
- Wire:
com.squareup.retrofit2:converter-wire
- Simple XML:
com.squareup.retrofit2:converter-simplexml
- Scalars (primitives, boxed, and String):
com.squareup.retrofit2:converter-scalars