美文网首页
Retrofit学习与使用(一)-GET请求

Retrofit学习与使用(一)-GET请求

作者: TRT131 | 来源:发表于2019-03-02 23:29 被阅读0次

    -简单使用

    建立接口

    //Call<ResponseBody> 表示返回值,@Query("q")表示get请求参数
    @GET("sug")
        Call<ResponseBody> listProduct(@Query("code") String code , @Query("q") String q);
    

    建立请求

    Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://suggest.taobao.com/") //表示请求的URL
                    .addConverterFactory(ScalarsConverterFactory.create()) //转化器,转化ResponseBody为我们想要的类型
                    .addConverterFactory(GsonConverterFactory.create())//同上
                    .build();
            GetRequest_Interface request=retrofit.create(GetRequest_Interface.class);//用于获取代理对象
            Call<ResponseBody> repos=request.listProduct("utf-8","卫衣");//发起请求
            repos.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        Log.i("ResponseBody", "onResponse: " + response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
    
                }
            });
    

    返回的值

    onResponse:  {"result":[["卫衣女","14224035"],["卫衣男","7802480"],["卫衣女宽松","9460542"],["卫衣女宽松韩版","9161180"],["卫衣女2019新款潮","1749160"],["卫衣女潮ins","380324"],["卫衣男潮","11334008"],["卫衣男连帽","11125611"],["卫衣女连帽","12248768"],["卫衣女春秋","12367502"]]}
    
    

    -将返回体解析成JSON字符串

    //Call<String> 表示返回值,当前需要解析出JSON字符串,@QueryMap 里存放了请求的参数
    @GET("sug")
        Call<String> listProduct(@QueryMap HashMap<String,String> maps);
    
    Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://suggest.taobao.com/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            GetRequest_Interface request=retrofit.create(GetRequest_Interface.class);
            Map<String,String> maps=new HashMap<>();
            maps.put("code","utf-8");
            maps.put("q","卫衣");
            Call<String> repos=request.listProduct((HashMap<String, String>) maps);//与接口对应,用map提交get请求参数
            repos.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                        Log.i("ResponseBody", "onResponse: " + response.body());//返回体就是JSON字符串
                }
    
                @Override
                public void onFailure(Call<String> call, Throwable t) {
    
                }
            });
    

    -将返回体解析成JAVA对象

    public class ProductResult {
        public List<List<String>> getResult() {
            return result;
        }
    
        public void setResult(List<List<String>> result) {
            this.result = result;
        }
    
        private List<List<String>> result;
    
    }
    
    //Call<ProductResult> 表示返回值,当前需要解析出JAVA对象,@QueryMap 里存放了请求的参数
    @GET("sug")
        Call<ProductResult> listProduct(@QueryMap HashMap<String,String> maps);
    
    
    Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://suggest.taobao.com/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            GetRequest_Interface request=retrofit.create(GetRequest_Interface.class);
            Map<String,String> maps=new HashMap<>();
            maps.put("code","utf-8");
            maps.put("q","卫衣");
            Call<ProductResult> repos=request.listProduct((HashMap<String, String>) maps);
            repos.enqueue(new Callback<ProductResult>() {
                @Override
                public void onResponse(Call<ProductResult> call, Response<ProductResult> response) {
                        Log.i("ResponseBody", "onResponse: " + response.body());
                }
    
                @Override
                public void onFailure(Call<ProductResult> call, Throwable t) {
    
                }
            });
    

    返回值如下

    onResponse: com.example.trt.rxjavademo.ProductResult@a69b38b
    

    相关文章

      网友评论

          本文标题:Retrofit学习与使用(一)-GET请求

          本文链接:https://www.haomeiwen.com/subject/yygyuqtx.html