gpt4 book ai didi

android - 如何使用 Retrofit 发布 HTTPS 请求?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:52 26 4
gpt4 key购买 nike

我通过这个构建了一个 restAdapter:

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

然后在 SafeUserApi.class(这是一个接口(interface))中我有:

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

但它不起作用。我错过了什么?

我尝试使用 Postman,它有效。但在我的设备中它没有。

我导入了这四个库:

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')

最佳答案

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);
}

}

这应该有效:

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

关于android - 如何使用 Retrofit 发布 HTTPS 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27716001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com