How to post HTTPS request using Retrofit?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 10:09:36

问题


I build a restAdapter by this:

    return new RestAdapter.Builder()
            .setEndpoint("https://www.xyz.com/")
            .build()
            .create(SafeUserApi.class);

then in the SafeUserApi.class (which is an interface) I have:

public interface SafeUserApi {
    @POST("/api/userlogin")
    void getUserLogin(@Body UserserLogin userLogin, Callback<LoginResult> cb);
}

But it does not work. Did I miss something?

I tried with Postman and it works. But in my device it doesn't.

I have these four library imported:

compile files('libs/okhttp-2.1.0.jar')
compile files('libs/okhttp-urlconnection-2.1.0.jar')
compile files('libs/okio-1.0.1.jar')
compile files('libs/retrofit-1.8.0.jar')

回答1:


public static OkHttpClient getUnsafeOkHttpClient() {

    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[0];
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts,
                new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext
                .getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient = okHttpClient.newBuilder()
                .sslSocketFactory(sslSocketFactory)
                .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).build();

        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

This should work:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(YOUR_HTTPS_URL)
    .setClient(getUnsafeOkHttpClient())
    .build();



回答2:


public interface SafeUserApi {
    @FormUrlEncoded
    @POST("/api/userlogin")
    void getUserLogin(@Field("parm1")UserserLogin userLogin, Callback<LoginResult> cb);
}

Here parm1 is the POST parameter that you will be passing it to the server. This will solve your problem



来源:https://stackoverflow.com/questions/27716001/how-to-post-https-request-using-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!