post 改造请求在 android 中收到错误请求响应
post request with retrofit gets responce a s bad request in android
我在 android
使用 https://docs.ngenius-payments.com/reference#hosted-payment-page 付款
Headers:
将这些 headers 添加到您的请求中(请注意,您应该将 'your_api_key' 替换为“入门”部分中的服务帐户 API 密钥)。
Header 值
Content-Type application/vnd.ni-identity.v1+json
基本授权:your_api_key
Body / 表单数据:
将以下信息添加到请求的 form/body 内容中。
示例请求 (body):
JSON
{
‘realmName’: ‘ni’
}
这些是 headers 和内容类型,我使用改造
创建了一个 post 方法
public static Retrofit getRetrofitClient() {
//If condition to ensure we don't create multiple retrofit instances in a single application
if (retrofit == null) {
//Defining the Retrofit using Builder
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL) //This is the only mandatory call on Builder object.
.addConverterFactory(GsonConverterFactory.create()) // Convertor library used to convert response into POJO
.build();
}
return retrofit;
}
我的api界面是
@POST("identity/auth/access-token")
Call<NgeniusPaymentAccessTokenModel> nGeniusAccessToken(@Header("content-type") String ContentType, @Header("authorization") String apiKey, @Body JsonObject object);
我称它为
JsonObject postParam = new JsonObject();
try {
postParam.addProperty("realmName", "ni");
} catch (Exception e) {
e.printStackTrace();
}
Call call = apiService.nGeniusAccessToken(contentType, "Basic "+apiKey, postParam);
我得到的响应是错误的,告诉我这是一个错误的请求,如何解决这个问题
您可以试试下面的代码:
String contentType = "application/vnd.ni-identity.v1+json";
String authorization = "Basic: "+apiKey;
JSONObject postParam = new JSONObject();
try {
postParam.put("realmName", "ni");
} catch (JSONException e) {
e.printStackTrace();
}
Call call = apiService.nGeniusAccessToken(contentType, authorization, postParam);
这个对我有用,我把所有 header 放在 header 地图中
创建地图
Map<String, String> stringMap = new HashMap<>();
try {
stringMap.put("Authorization", "auth");
stringMap.put("Content-Type", "CONTENT_TYPE");
stringMap.put("accept", "accept");
} catch (Exception e) {
e.printStackTrace();
}
api界面看起来像
@POST("transactions/orders")
Call<ResponseBody> nCreateOrder(@HeaderMap Map<String, String> headers,String out, @Body JsonObject object);
现在用
来称呼它
nCreateOrder(stringMap ,"out",jsonObject);
我在 android
使用 https://docs.ngenius-payments.com/reference#hosted-payment-page 付款Headers: 将这些 headers 添加到您的请求中(请注意,您应该将 'your_api_key' 替换为“入门”部分中的服务帐户 API 密钥)。
Header 值 Content-Type application/vnd.ni-identity.v1+json
基本授权:your_api_key
Body / 表单数据: 将以下信息添加到请求的 form/body 内容中。
示例请求 (body): JSON { ‘realmName’: ‘ni’ }
这些是 headers 和内容类型,我使用改造
创建了一个 post 方法 public static Retrofit getRetrofitClient() {
//If condition to ensure we don't create multiple retrofit instances in a single application
if (retrofit == null) {
//Defining the Retrofit using Builder
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL) //This is the only mandatory call on Builder object.
.addConverterFactory(GsonConverterFactory.create()) // Convertor library used to convert response into POJO
.build();
}
return retrofit;
}
我的api界面是
@POST("identity/auth/access-token")
Call<NgeniusPaymentAccessTokenModel> nGeniusAccessToken(@Header("content-type") String ContentType, @Header("authorization") String apiKey, @Body JsonObject object);
我称它为
JsonObject postParam = new JsonObject();
try {
postParam.addProperty("realmName", "ni");
} catch (Exception e) {
e.printStackTrace();
}
Call call = apiService.nGeniusAccessToken(contentType, "Basic "+apiKey, postParam);
我得到的响应是错误的,告诉我这是一个错误的请求,如何解决这个问题
您可以试试下面的代码:
String contentType = "application/vnd.ni-identity.v1+json";
String authorization = "Basic: "+apiKey;
JSONObject postParam = new JSONObject();
try {
postParam.put("realmName", "ni");
} catch (JSONException e) {
e.printStackTrace();
}
Call call = apiService.nGeniusAccessToken(contentType, authorization, postParam);
这个对我有用,我把所有 header 放在 header 地图中
创建地图
Map<String, String> stringMap = new HashMap<>();
try {
stringMap.put("Authorization", "auth");
stringMap.put("Content-Type", "CONTENT_TYPE");
stringMap.put("accept", "accept");
} catch (Exception e) {
e.printStackTrace();
}
api界面看起来像
@POST("transactions/orders")
Call<ResponseBody> nCreateOrder(@HeaderMap Map<String, String> headers,String out, @Body JsonObject object);
现在用
来称呼它 nCreateOrder(stringMap ,"out",jsonObject);