Retrofit入门

1   Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create()) //请求返回字符串,如需返回对象,需使用converter-gson
.baseUrl("http://www.baidu.com").build();
DataService service = retrofit.create(DataService.class);
 public interface DataService {
@GET("/")
Call<String> getData();
}
 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) { }
});
  • Gsoncom.squareup.retrofit2:converter-gson
  • Jacksoncom.squareup.retrofit2:converter-jackson
  • Moshicom.squareup.retrofit2:converter-moshi
  • Protobufcom.squareup.retrofit2:converter-protobuf
  • Wirecom.squareup.retrofit2:converter-wire
  • Simple XMLcom.squareup.retrofit2:converter-simplexml
  • Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
上一篇:[ios2]tableView去除空行的singleLine


下一篇:Retrofit 从入门到了解【总结】