标签:
1、配置:
compile ‘com.squareup.retrofit:retrofit:2.0.0-beta2‘
compile ‘com.squareup.retrofit:converter-gson:2.0.0-beta2‘
2、定义服务和返回值
public interface LoginService { @POST("/test") Call<LoginResponse> loginHandler(@Query("username")String username, @Query("username")String password); } public class LoginResponse{ private String result; public String getResult(){ return this.result; } }
3、使用网络接口如下:
fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.0.12:3000") .addConverterFactory(GsonConverterFactory.create()) .build(); LoginService service = retrofit.create(LoginService.class); Call<LoginResponse> call = service.loginHandler("fredric", "fredric"); call.enqueue(new Callback<LoginResponse>() { @Override public void onResponse(Response<LoginResponse> response, Retrofit retrofit) { System.out.println(response.body().getResult()); } @Override public void onFailure(Throwable t) { System.out.println("err msg:"+t.getMessage()); } }); } });
标签:
原文地址:http://www.cnblogs.com/Fredric-2013/p/5382031.html