我正在使用改造2实现登录发布方法。它在Postman中运行良好但返回500错误。我尝试过不同的解决方案,在StackOverFlow中搜索的解决方案对我来说没有用,所以这是我的实现:
@FormUrlEncoded
@POST("mobile/login")
Call<JsonObject> login(@Field(value = "_username") String username, @Field(value = "_password") String password);
登录
public void login(String _username, String _password, final NetworkRequestObjectResult callBackResult) {
mRoomPresenceAPI = provideApiService();
Call<JsonObject> callObject = mRoomPresenceAPI.login(_username, _password);
callObject.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.body() != null && response.code() == 200) {
Log.i(TAG, "mobile/login Success code: "+response.code() + " content: "+response.body().toString());
callBackResult.onSuccess(null);
} else {
if (response.errorBody() != null) {
Log.e(TAG, "mobile/login error code: "+response.code() + " error content: "+response.errorBody().toString());
//general error
callBackResult.onError(mContext.getResources().getString(R.string.error_login));
}
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e(TAG, "mobile/login error : "+t.getMessage());
callBackResult.onError(mContext.getResources().getString(R.string.error_login));
}
});
}
提供服务
public INetworkAPI provideApiService() {
return provideRetrofit(BASE_URL, provideClient()).create(INetworkAPI.class);
}
//provide client
public OkHttpClient provideClient() {
loadSSLConfig();
OkHttpClient.Builder clientBuilder = getUnsafeOkHttpClient();
clientBuilder.addInterceptor(new ResponseInterceptor());
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(loggingInterceptor);
return clientBuilder.build();
}
class ResponseInterceptor implements Interceptor {
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request());
return response.newBuilder()
}
}
private void loadSSLConfig() {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
InputStream inputStream = mContext.getApplicationContext().getAssets().open("roompresencecom_cert.crt");
//new BufferedInputStream(new FileInputStream("roompresencecom_cert.crt"));
// mContext.getResources().openRawResource(R.raw.roompresencecom_cert);
Certificate certificate = certificateFactory.generateCertificate(inputStream);
inputStream.close();
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", certificate);
// Create a TrustManager that trusts the CAs in our KeyStore.
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(tmfAlgorithm);
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
x509TrustManager = (X509TrustManager) trustManagers[0];
// Create an SSLSocketFactory that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{x509TrustManager}, null);
sslSocketFactory = sslContext.getSocketFactory();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static OkHttpClient.Builder 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[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
提供改造 public Retrofit provideRetrofit(String baseURL,OkHttpClient client){
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setLenient();
gson = gsonBuilder.create();
return new Retrofit.Builder()
.baseUrl(baseURL)
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
基本网址 public final String BASE_URL =&#34; https://sandbox.roompresence.com/&#34 ;;
[Postman][1]
答案 0 :(得分:0)
Call<JsonObject> login(@Field("_username") String username, @Field("_password") String password);