我正在使用retreate 2实现一个登录post方法。它在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);
}
}
提供改造公共改造提供改造(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();
}
base URL public最终字符串BASE_URL="https://sandbox.roompresence.com/";
[Postman][1]
问题已解决(服务器问题)。需要特别注意的是:在API中,这个请求在内部被重定向到web登录请求,这个重定向在POstman和IOS中表现良好,但不是来自Android(我开发web版本的同事用Fiddler检测到了这一点)。这个请求也不属于我正在使用的移动API,所以问题可能就在那里。
解决方案是向移动API添加一个新的登录请求,瞧,它的工作原理。
改装请求:
@POST("mobile/login")
Call<JsonObject> login(@Body Map<String,String> parameters);
试试这个,希望对你有帮助!
Call<JsonObject> login(@Field("_username") String username, @Field("_password") String password);
我的签名 改装类别:
我想用不同的预请求脚本多次运行相同的请求?不使用数据驱动(CSV)测试,我能做什么? 例如,我必须多次运行下面的GET url(每2分钟一次),但每当我运行它时,我都需要进行不同的预请求测试! {{url}}/legacy/COL 在此处输入图像描述
我正在做一个关于林达的烧瓶课程,但遇到了一个例子的问题。https://www.lynda.com/Flask-tutorials/Web-API-Development-Flask/521200-2.html 我做一个简单的职位,它的工作时,从一个基本的网络表单,但从邮递员失败。我想这是因为邮递员对请求的编码不同,但不知道为什么会这样,或者为什么烧瓶不能读取它。 查理斯截获的请求: 返回: 从简
我刚刚开始使用Postman测试我正在集成到的API。 我有以下错误不断出现 无效的CORS请求 注意以下几点: API使用承载令牌身份验证(OAuth2)。我有这个工作没有问题 到目前为止,我发现: 使用邮递员与Http POST请求-我没有得到粗体的部分 以防其他人遇到同样的问题,以下是解决方法。在chrome浏览器中https://www.getpostman.com/docs/captur
我有一个对服务器的Authenticate post调用,如下所示: 用这样的尸体: 我总是必须在Postman中执行两次这个Authenticate调用,以正确填充我的全局var“安全令牌”,并在此后的下一次调用中使用该令牌进行身份验证,因此,似乎预请求脚本实际上是在脚本之后运行的,或者是预请求脚本中设置的全局var对当前请求不容易可用? 我做错了什么? 以下是预先请求的SRIPT:
我正在开发一个Registraion API,它接受以下/request perameters的json输入 但是当我从android应用程序调用时,它给出了错误的响应,错误代码为,但在Postman上工作得很好。 我的API客户端 衷心感谢