Retrofit2.0 使用教程

版权声明,转载请著名出处:http://www.jianshu.com/p/73a803b0b026

引言

俗话说:代码是程序员的最好的教程,这篇文章记录的是我在学习使用Retrofit的代码笔记,其中里面的很多注解或原理我也没有弄明白,但是这不影响我的正常使用啊,当然这篇文章针对的是初学者,如果是老司机的话就请绕道了,如果大家有更好的看法或建议可以在文末进行评论,我会及时更新到文章中,近期我也会更新RxJava的简单使用和RxJava结合Retrofit使用的教程,希望大家喜欢,欢迎大家持续关注!

开始使用

导入依赖

    //  retrofit的依赖
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    //将JSON字符串转换为对象需要使用的依赖
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'

IpMode类由Json字符串GsonFromat生成

{
    "code": 0,
    "data": {
        "country": "中国",
        "country_id": "CN",
        "area": "西南",
        "area_id": "500000",
        "region": "四川省",
        "region_id": "510000",
        "city": "成都市",
        "city_id": "510100",
        "county": "",
        "county_id": "-1",
        "isp": "电信",
        "isp_id": "100017",
        "ip": "118.114.168.175"
    }
}

1. Get简单请求

注:Retrofit2 的baseUlr 必须以 /(斜线) 结束,不然会抛出一个IllegalArgumentException,所以如果你看到别的教程没有以 / 结束,那么多半是直接从Retrofit 1.X 照搬过来的。

//请求的接口
public interface IpService {
    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: MoonRetrofit"  //必须要有这个Hearder,否则报错 java.lang.IllegalArgumentException: @Headers annotation is empty.
    })
    @GET("getIpInfo.php?ip=59.108.54.37")
    Call<IpMode> getIpMsgGet();
    /**
     * 采用指定的全路径访问
     * @param url   网址的全路径
     * @return
     */
    @Headers({
            "User-Agent: MoonRetrofit"
    })
    @GET
    Call<IpMode> getIpMsgGet1(@Url String url);
}
 public void getSimple(View view) {
        // Get简单请求
        final String url = "http://ip.taobao.com/service/";
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<IpMode> ipMsg = ipService.getIpMsgGet();
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
        getSimple();// Get简单请求 ,采用全路径来访问
    }
    private void getSimple() {
        // Get简单请求 ,采用全路径来访问
        final String url = "http://ip.taobao.com/service/";
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<IpMode> ipMsg = ipService.getIpMsgGet1("http://ip.taobao.com/service/getIpInfo.php?ip=118.114.168.175");
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

2. Get带参数请求(路径)可以通过参数动态修改网址的路径

    /**
     * 指定路径的访问
     *
     * @param pathStr 路径
     * @return
     */
    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: MoonRetrofit"
    })
    @GET("{path}/getIpInfo.php?ip=59.108.54.37")//{path}与@Path("path")参数对应,接受参数后{path}被替换
    Call<IpMode> getIpMsgGet(@Path("path") String pathStr);
    public void getParamesPath(View view) {
//        Get带参数请求(路径)
        final String baseUrl = "http://ip.taobao.com/";
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<IpMode> ipMsg = ipService.getIpMsgGet("service");   //,"getIpInfo.php?ip=59.108.54.37"
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

3. Get带参数请求(IP)可以动态修改网址?后面的参数

    /**
     * 指定IP访问
     * 会组成全路径:http://ip.taobao.com/service/getIpInfo.php?ip=118.114.168.175
     * 在路径后自动添加  ?ip=ipStr
     *
     * @param ipStr
     * @return
     */
    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: MoonRetrofit"
    })
    @GET("service/getIpInfo.php")
    Call<IpMode> getIpMsgFromIpGet(@Query("ip") String ipStr);
    public void getParamesIp(View view) {
//        Get带参数请求(IP)
        final String baseUrl = "http://ip.taobao.com/";
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<IpMode> ipMsg = ipService.getIpMsgFromIpGet("59.108.54.37");   //,"getIpInfo.php?ip=59.108.54.37"
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

4. Get带参数请求(路径和IP)可以通过参数动态修改网址的路径和网址?后面的请求参数

    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: MoonRetrofit"
    })
    @GET("{path}")
    Call<IpMode> getIpMsgGet(@Path("path") String pathStr, @Query("ip") String ipStr);
    public void getParamesPathIp(View view) {
//        Get带参数请求(路径和IP)
        final String baseUrl = "http://ip.taobao.com/";
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<IpMode> ipMsg = ipService.getIpMsgGet("service/getIpInfo.php", "59.108.54.37");   //,"getIpInfo.php?ip=59.108.54.37"
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

5.Get带参数请求(动态指定查询条件组)

    /**
     * 动态指定查询条件组
     *
     * @param quaryMap
     * @return
     */
    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: MoonRetrofit"
    })
    @GET("service/getIpInfo.php")
    Call<IpMode> getIpMsgGet(@QueryMap Map<String, String> quaryMap);
    public void getParamesQuaryMap(View view) {
//        Get带参数请求(动态指定查询条件组)
        final String baseUrl = "http://ip.taobao.com/";
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("ip", "59.108.54.37"); //Key 和 Value要对应,其中Key名要与网站对应
        Call<IpMode> ipMsg = ipService.getIpMsgGet(hashMap);
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

6.Post带参数请求

    /**
     * 带参数的POST请求
     * 用@Field("ip")标注请求的参数键值
     *
     * @param ip
     * @return
     */
    @Headers({
            "Accept-Encoding: application/json", //
            "User-Agent: MoonRetrofit"
    })
    @FormUrlEncoded  //注明是一个表单请求
    @POST("service/getIpInfo.php")
    Call<IpMode> getIpMsgPost(@Field("ip") String ip);
    @Headers({
            "Accept-Encoding: application/json", //
            "User-Agent: MoonRetrofit"
    })
    @FormUrlEncoded  //注明是一个表单请求
    @POST("service/getIpInfo.php")
    Call<IpMode> getIpMsgPost(@FieldMap Map<String, String> quaryMap);
    public void postParames(View view) {
//        Post带参数请求
        String url = "http://ip.taobao.com/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
//        Call<IpMode> ipMsg = ipService.getIpMsgPost("59.108.54.37");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("ip", "59.108.54.37");
        Call<IpMode> ipMsg = ipService.getIpMsgPost(hashMap);
        ipMsg.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                IpMode ipMode = response.body();
                Log.e(TAG, "onResponse: " + ipMode);
                Toast.makeText(MainActivity.this, ipMode + "", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

7. 下载图片或文件

    /**
     * 下载图片,返回的是OkHttp库的Call<ResponseBody>, ResponseBody里面是字节数据
     * 百度的首页图
     *
     * @return
     */
    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: MoonRetrofit"
    })
    @Streaming
    @GET("5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png")
    Call<ResponseBody> getImgMsgGet();
    /**
     * 使用全路径的网址下载图片
     *
     * @param fileUrl
     * @return
     */
    @GET
    Call<ResponseBody> getImgMsgGet(@Url String fileUrl);
    public void getDownloadImg(View view) {
//        Get下载图片
        String url = "https://ss0.bdstatic.com/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<ResponseBody> imgMsgGet = ipService.getImgMsgGet();
        imgMsgGet.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    byte[] bytes = response.body().bytes();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    imageview.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.e(TAG, "onResponse: 图片请求成功");
                Toast.makeText(MainActivity.this, "图片请求成功", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
        //使用全路径
        Call<ResponseBody> imgMsgGet2 = ipService.getImgMsgGet("http://img1.3lian.com/2015/a1/105/d/40.jpg");
        imgMsgGet2.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                File file = new File(MainActivity.this.getCacheDir(), "laowang.jpg");
                try {
                    byte[] bytes = response.body().bytes();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    imageview.setImageBitmap(bitmap);
                    //保存为文件
                    FileOutputStream fileOutputStream = new FileOutputStream(file, false);
                    fileOutputStream.write(bytes);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.e(TAG, "onResponse: 图片请求成功2,ex=" + file.exists() + ",path=" + file.getAbsolutePath());
                Toast.makeText(MainActivity.this, "图片请求成功2", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e(TAG, "onFailure: 2");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "2", Toast.LENGTH_LONG).show();
            }
        });
    }

8. POST上传文件

    /**
     *  POST上传文件
     *  注解@Multipart 表示允许多个@Part
     *  如需要多个文件上传可以使用@PartMap
     * @param file
     * @return
     */
    @Multipart  
    @POST("fileService")
    Call<IpMode> uploadFile(@Part MultipartBody.Part file);
    public void postUpImg(View view) {// POST上传图片
        File file = new File(MainActivity.this.getCacheDir(), "laowang.jpg");
        RequestBody body = RequestBody.create(MediaType.parse("application/otcet-stream"), file);//构建文件的RequestBody
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);//构建文件的Part
        String url = "https://ss0.bdstatic.com/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call<IpMode> call = ipService.uploadFile(part);
        call.enqueue(new Callback<IpMode>() {
            @Override
            public void onResponse(Call<IpMode> call, Response<IpMode> response) {
                Log.e(TAG, "onResponse: 图片上传成功," + response.headers());
                Toast.makeText(MainActivity.this, "图片上传成功", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<IpMode> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
                t.printStackTrace();
                Toast.makeText(MainActivity.this, t + "", Toast.LENGTH_LONG).show();
            }
        });
    }

我的CSDN博客地址:http://blog.csdn.net/wo_ha/article/details/77866493

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,653评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,321评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,833评论 0 324
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,472评论 1 266
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,306评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,274评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,658评论 3 385
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,335评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,638评论 1 293
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,697评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,454评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,311评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,699评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,986评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,254评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,647评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,847评论 2 335

推荐阅读更多精彩内容