Retrofit

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        http://api.svipmovie.com/front/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://www.zhaoapi.cn")
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();



//        testPath(retrofit);


        GitHubService service =  retrofit.create(GitHubService.class);

        Call<String> call =  service.stringGet();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
               String result =  response.body() ;
               System.out.println(Thread.currentThread().getName());

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

//        Call<LoginBean> call =  service.url("/user/login?mobile=18588888888&password=111111");
//
//        call.enqueue(new Callback<LoginBean>() {
//            @Override
//            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
//                LoginBean bean =  response.body() ;
//                System.out.println(Thread.currentThread().getName());
//
//            }
//
//            @Override
//            public void onFailure(Call<LoginBean> call, Throwable t) {
//
//            }
//        });


//        Call<LoginBean> call =  service.login("18588888888","111111");

//        Map<String,String> map = new HashMap<>();
//        map.put("mobile","18588888888");
//        map.put("password","111111");
//
//
//        Call<ResponseBody> call =  service.postMap1(map);
//
//        call.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                try {
//                    String resu =  response.body().string() ;
//                                    System.out.println(Thread.currentThread().getName());
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//
//            }
//        });

//
//        Call<LoginBean> call = service.postBody(new Login("18588888888","111111"));
//
//        call.enqueue(new Callback<LoginBean>() {
//            @Override
//            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
//                LoginBean bean =  response.body() ;
//                System.out.println(Thread.currentThread().getName());
//            }
//
//            @Override
//            public void onFailure(Call<LoginBean> call, Throwable t) {
//
//            }
//        });

//        Call<Bean> call =  service.testGetParams("d8628ea824ca4ae9af3cfc0d4afed0a9");
//
//        call.enqueue(new Callback<Bean>() {
//            @Override
//            public void onResponse(Call<Bean> call, Response<Bean> response) {
//
//                Bean bean =  response.body() ;
//                System.out.println(Thread.currentThread().getName());
//
//            }
//
//            @Override
//            public void onFailure(Call<Bean> call, Throwable t) {
//
//            }
//        });


    }



    public void testIpPath(Retrofit retrofit){
        GitHubService service =  retrofit.create(GitHubService.class);

        Call<BweiBean> call =  service.testBwei("Bwei");

        call.enqueue(new Callback<BweiBean>() {
            @Override
            public void onResponse(Call<BweiBean> call, Response<BweiBean> response) {
                BweiBean bean =  response.body() ;
                System.out.println(Thread.currentThread().getName());

            }

            @Override
            public void onFailure(Call<BweiBean> call, Throwable t) {
                System.out.println(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName());

            }
        });
    }


    private void testPath(Retrofit retrofit){
        GitHubService gitHubService =  retrofit.create(GitHubService.class);

        Call<Bean> call =  gitHubService.test1509D("front");

        call.enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                Bean bean =  response.body() ;
                System.out.println(Thread.currentThread().getName());
            }

            @Override
            public void onFailure(Call<Bean> call, Throwable t) {

            }
        });

    }
}
接口

public interface GitHubService {
//
//    Path 路径替换
//    GET get 请求
    @GET("{user}/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9")
    Call<Bean> test1509D(@Path("user") String user);


    @GET("/{test}/news")
    Call<BweiBean> testBwei(@Path("test") String test);


    @GET("/Bwei/news")
    Call<BweiBean> testGet();
//    d8628ea824ca4ae9af3cfc0d4afed0a9
//    Query get 请求带参数
    @GET("front/videoDetailApi/videoDetail.do")
    Call<Bean> testGetParams(@Query("mediaId") String value);


//    https://www.zhaoapi.cn/user/login?mobile=18588888888&password=111111

    @GET("/user/login")
    Call<LoginBean> login(@Query("mobile") String mobile,@Query("password") String password);


    @GET("/user/login")
    Call<LoginBean> loginMap(@QueryMap Map<String,String> map);


    @FormUrlEncoded
    @POST("/user/login")
    Call<LoginBean> post(@Field("mobile") String mobile,@Field("password") String password);


    @FormUrlEncoded
    @POST("/user/login")
    Call<LoginBean> postMap(@FieldMap Map<String,String> map);


    @Headers({
            "Cache-Control: max-age=640000",
            "Accept: application/vnd.github.v3.full+json"
    })
    @POST("/user/login")
    Call<LoginBean> postBody(@Body Login login);


    @FormUrlEncoded
    @POST("/user/login")
    Call<ResponseBody> postMap1(@FieldMap Map<String,String> map);



    @GET
    Call<LoginBean> url(@Url String url);


    @GET("/user/login?mobile=18588888888&password=111111")
    Call<String> stringGet();

//
//    @GET
//    Call<String> getAll(@Url String url,@QueryMap Map<String,String> map);
//
//    @POST
//    Call<String> postAll(@Url String url , @FieldMap Map<String,String> map);
//
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Retrofit是一个非常流行的网络请求库,支持HTTP和HTTPS协议。在使用Retrofit进行HTTPS请求时,需要进行一些配置,以确保请求的安全性。下面是使用Retrofit进行HTTPS请求的步骤: 1.在build.gradle文件中添加Retrofit和OkHttp的依赖: ```gradle implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.okhttp3:okhttp:4.9.0' ``` 2.创建OkHttpClient实例,并配置TLS: ```java OkHttpClient.Builder builder = new OkHttpClient.Builder(); ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .cipherSuites( CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256) .build(); builder.connectionSpecs(Collections.singletonList(spec)); OkHttpClient client = builder.build(); ``` 3.创建Retrofit实例,并将OkHttpClient实例传入: ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .client(client) .build(); ``` 4.创建API接口,并使用@HTTPS注解指定请求的HTTPS证书: ```java public interface ApiService { @GET("/") @HTTPS(certificates = "cert.pem") Call<String> get(); } ``` 5.发送HTTPS请求: ```java ApiService apiService = retrofit.create(ApiService.class); Call<String> call = apiService.get(); Response<String> response = call.execute(); String result = response.body(); ``` 需要注意的是,以上步骤只是简单介绍了如何使用Retrofit进行HTTPS请求,实际使用中还需要根据具体情况进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值