我建立了一个restAdapter:
return new RestAdapter.Builder()
.setEndpoint("https://www.xyz.com/")
.build()
.create(SafeUserApi.class);
然后在SafeUserApi.class(这是一个接口)我有:
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')
答案 0 :(得分:14)
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();
答案 1 :(得分:-3)
public interface SafeUserApi {
@FormUrlEncoded
@POST("/api/userlogin")
void getUserLogin(@Field("parm1")UserserLogin userLogin, Callback<LoginResult> cb);
}
这里parm1是POST参数,您将把它传递给服务器。 这将解决您的问题