Retrofit

 

Retrofit

  99人阅读  评论(0)  收藏  举报

MainActivity.java

[html]  view plain  copy
  1. import android.support.v7.app.AppCompatActivity;  
  2. import android.os.Bundle;  
  3. import android.widget.Toast;  
  4.   
  5. import java.io.IOException;  
  6. import java.util.HashMap;  
  7. import java.util.Map;  
  8.   
  9. import okhttp3.ResponseBody;  
  10. import retrofit2.Call;  
  11. import retrofit2.Callback;  
  12. import retrofit2.Response;  
  13. import retrofit2.Retrofit;  
  14. import retrofit2.converter.gson.GsonConverterFactory;  
  15. import retrofit2.converter.scalars.ScalarsConverterFactory;  
  16.   
  17. public class MainActivity extends AppCompatActivity {  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23. //        http://api.svipmovie.com/front/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9  
  24.   
  25.         Retrofit retrofit = new Retrofit.Builder()  
  26.                 .baseUrl("https://www.zhaoapi.cn")  
  27. //                .addConverterFactory(GsonConverterFactory.create())  
  28.                 .addConverterFactory(ScalarsConverterFactory.create())  
  29.                 .build();  
  30.   
  31.   
  32.   
  33. //        testPath(retrofit);  
  34.   
  35.   
  36.         GitHubService service =  retrofit.create(GitHubService.class);  
  37.   
  38.         Call<String> call =  service.stringGet();  
  39.   
  40.         call.enqueue(new Callback<String>() {  
  41.             @Override  
  42.             public void onResponse(Call<String> call, Response<String> response) {  
  43.                String result =  response.body() ;  
  44.                System.out.println(Thread.currentThread().getName());  
  45.   
  46.             }  
  47.   
  48.             @Override  
  49.             public void onFailure(Call<String> call, Throwable t) {  
  50.   
  51.             }  
  52.         });  
  53.   
  54. //        Call<LoginBean> call =  service.url("/user/login?mobile=18588888888&password=111111");  
  55. //  
  56. //        call.enqueue(new Callback<LoginBean>() {  
  57. //            @Override  
  58. //            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {  
  59. //                LoginBean bean =  response.body() ;  
  60. //                System.out.println(Thread.currentThread().getName());  
  61. //  
  62. //            }  
  63. //  
  64. //            @Override  
  65. //            public void onFailure(Call<LoginBean> call, Throwable t) {  
  66. //  
  67. //            }  
  68. //        });  
  69.   
  70.   
  71. //        Call<LoginBean> call =  service.login("18588888888","111111");  
  72.   
  73. //        Map<String,String> map = new HashMap<>();  
  74. //        map.put("mobile","18588888888");  
  75. //        map.put("password","111111");  
  76. //  
  77. //  
  78. //        Call<ResponseBody> call =  service.postMap1(map);  
  79. //  
  80. //        call.enqueue(new Callback<ResponseBody>() {  
  81. //            @Override  
  82. //            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {  
  83. //                try {  
  84. //                    String resu =  response.body().string() ;  
  85. //                                    System.out.println(Thread.currentThread().getName());  
  86. //  
  87. //                } catch (IOException e) {  
  88. //                    e.printStackTrace();  
  89. //                }  
  90. //            }  
  91. //  
  92. //            @Override  
  93. //            public void onFailure(Call<ResponseBody> call, Throwable t) {  
  94. //  
  95. //            }  
  96. //        });  
  97.   
  98. //  
  99. //        Call<LoginBean> call = service.postBody(new Login("18588888888","111111"));  
  100. //  
  101. //        call.enqueue(new Callback<LoginBean>() {  
  102. //            @Override  
  103. //            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {  
  104. //                LoginBean bean =  response.body() ;  
  105. //                System.out.println(Thread.currentThread().getName());  
  106. //            }  
  107. //  
  108. //            @Override  
  109. //            public void onFailure(Call<LoginBean> call, Throwable t) {  
  110. //  
  111. //            }  
  112. //        });  
  113.   
  114. //        Call<Bean> call =  service.testGetParams("d8628ea824ca4ae9af3cfc0d4afed0a9");  
  115. //  
  116. //        call.enqueue(new Callback<Bean>() {  
  117. //            @Override  
  118. //            public void onResponse(Call<Bean> call, Response<Bean> response) {  
  119. //  
  120. //                Bean bean =  response.body() ;  
  121. //                System.out.println(Thread.currentThread().getName());  
  122. //  
  123. //            }  
  124. //  
  125. //            @Override  
  126. //            public void onFailure(Call<Bean> call, Throwable t) {  
  127. //  
  128. //            }  
  129. //        });  
  130.   
  131.   
  132.     }  
  133.   
  134.   
  135.   
  136.     public void testIpPath(Retrofit retrofit){  
  137.         GitHubService service =  retrofit.create(GitHubService.class);  
  138.   
  139.         Call<BweiBean> call =  service.testBwei("Bwei");  
  140.   
  141.         call.enqueue(new Callback<BweiBean>() {  
  142.             @Override  
  143.             public void onResponse(Call<BweiBean> call, Response<BweiBean> response) {  
  144.                 BweiBean bean =  response.body() ;  
  145.                 System.out.println(Thread.currentThread().getName());  
  146.   
  147.             }  
  148.   
  149.             @Override  
  150.             public void onFailure(Call<BweiBean> call, Throwable t) {  
  151.                 System.out.println(Thread.currentThread().getName());  
  152.                 System.out.println(Thread.currentThread().getName());  
  153.                 System.out.println(Thread.currentThread().getName());  
  154.                 System.out.println(Thread.currentThread().getName());  
  155.                 System.out.println(Thread.currentThread().getName());  
  156.   
  157.             }  
  158.         });  
  159.     }  
  160.   
  161.   
  162.     private void testPath(Retrofit retrofit){  
  163.         GitHubService gitHubService =  retrofit.create(GitHubService.class);  
  164.   
  165.         Call<Bean> call =  gitHubService.test1509D("front");  
  166.   
  167.         call.enqueue(new Callback<Bean>() {  
  168.             @Override  
  169.             public void onResponse(Call<Bean> call, Response<Bean> response) {  
  170.                 Bean bean =  response.body() ;  
  171.                 System.out.println(Thread.currentThread().getName());  
  172.             }  
  173.   
  174.             @Override  
  175.             public void onFailure(Call<Bean> call, Throwable t) {  
  176.   
  177.             }  
  178.         });  
  179.   
  180.     }  
  181. }  
接口

[html]  view plain  copy
  1. public interface GitHubService {  
  2. //  
  3. //    Path 路径替换  
  4. //    GET get 请求  
  5.     @GET("{user}/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9")  
  6.     Call<Bean> test1509D(@Path("user") String user);  
  7.   
  8.   
  9.     @GET("/{test}/news")  
  10.     Call<BweiBean> testBwei(@Path("test") String test);  
  11.   
  12.   
  13.     @GET("/Bwei/news")  
  14.     Call<BweiBean> testGet();  
  15. //    d8628ea824ca4ae9af3cfc0d4afed0a9  
  16. //    Query get 请求带参数  
  17.     @GET("front/videoDetailApi/videoDetail.do")  
  18.     Call<Bean> testGetParams(@Query("mediaId") String value);  
  19.   
  20.   
  21. //    https://www.zhaoapi.cn/user/login?mobile=18588888888&password=111111  
  22.   
  23.     @GET("/user/login")  
  24.     Call<LoginBean> login(@Query("mobile") String mobile,@Query("password") String password);  
  25.   
  26.   
  27.     @GET("/user/login")  
  28.     Call<LoginBean> loginMap(@QueryMap Map<String,String> map);  
  29.   
  30.   
  31.     @FormUrlEncoded  
  32.     @POST("/user/login")  
  33.     Call<LoginBean> post(@Field("mobile") String mobile,@Field("password") String password);  
  34.   
  35.   
  36.     @FormUrlEncoded  
  37.     @POST("/user/login")  
  38.     Call<LoginBean> postMap(@FieldMap Map<String,String> map);  
  39.   
  40.   
  41.     @Headers({  
  42.             "Cache-Control: max-age=640000",  
  43.             "Accept: application/vnd.github.v3.full+json"  
  44.     })  
  45.     @POST("/user/login")  
  46.     Call<LoginBean> postBody(@Body Login login);  
  47.   
  48.   
  49.     @FormUrlEncoded  
  50.     @POST("/user/login")  
  51.     Call<ResponseBody> postMap1(@FieldMap Map<String,String> map);  
  52.   
  53.   
  54.   
  55.     @GET  
  56.     Call<LoginBean> url(@Url String url);  
  57.   
  58.   
  59.     @GET("/user/login?mobile=18588888888&password=111111")  
  60.     Call<String> stringGet();  
  61.   
  62. //  
  63. //    @GET  
  64. //    Call<String> getAll(@Url String url,@QueryMap Map<String,String> map);  
  65. //  
  66. //    @POST  
  67. //    Call<String> postAll(@Url String url , @FieldMap Map<String,String> map);  
  68. //  
  69. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值