Android Retrofit 入门教程

首先添加依赖

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'//请求结果直接转化为实体类,省略gson转化

创建一个接口

// 完整地址: http://www.wuhaojun.com/api/android/customer?type=1
public interface CustomerService {
    @GET("/api/android/customer")//Get请求地址
    Call<Customer> getCustomer(@Query("type") int type);//定义参数type的当前是第几页 1,2,3 ...
}

请求方法

        String baseUrl = "http://www.wuhaojun.com/";//请求地址,固定的一部分
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())//请求结果直接转化实体类
                .build();

        CustomerService movieService = retrofit.create(CustomerService.class);//创建对象
        Call<Customer> call = movieService.getCustomer(1);//传递请求参数 对应接口中的定义
        call.enqueue(new Callback<Customer>() {
            @Override
            public void onResponse(Call<Customer> call, Response<Customer> response) {
                Log.i(TAG, "onResponse: "+response.body().getMessage());//返回的就是实体类,不需要Gson转换
            }

            @Override
            public void onFailure(Call<Customer> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t.getMessage());
            }
        });

最后,别忘了添加网络权限

<uses-permission android:name="android.permission.INTERNET" />

附加其他上传类型

//post json
    @POST
    Call<String> reJson(@Url String url, @Body RequestBody body);

    HashMap<String, String> params = new HashMap<>();
    params.put("phone_num", "123");
    JSONObject json = new JSONObject(params);
    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), json);//转化类型


//post params
    @POST
    @FormUrlEncoded
    Call<String> reParams(@Url String url, @FieldMap Map<String, String> map);

    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("user", "test");    
    
    
//post file map
    @POST
    @Multipart
    Call<String> reUploadFile(@Url String url, @PartMap Map<String, RequestBody> params);
    
    File file = new File("");
    Map<String, RequestBody> hashMap = new HashMap<>();
    hashMap.put("json", RequestBody.create(MediaType.parse("text/plain"), "jsonargs"));//键值对
    hashMap.put("file\"; filename=\"" + file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));//文件

大神tough1985文章:http://gank.io/post/56e80c2c677659311bed9841

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,807评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,766评论 19 139
  • 最近在一边学习iOS开发,一边在尝试集成Vungle的SDK。 以下是一些有关Vungle SDK奖励回调机制的详...
    账房先生2016阅读 5,870评论 0 50
  • Efficient Usage 大多数介绍eclipse高效操作技巧的文章都是在介绍快捷键,本文也不例外!但如果仅...
    MagicBowen阅读 9,345评论 4 11
  • 年华啊时光啊,青春啊爱情啊,突然就想这么喊喊,好像这么喊喊它们就又倏尔流淌过。 在某一个记不清的时间节点之前,特别...
    薄情的世界深情地活着阅读 2,784评论 0 1