经过改造的发布请求会在android

时间:2020-03-26 19:50:19

标签: android post http-headers retrofit

我正在使用https://docs.ngenius-payments.com/reference#hosted-payment-page在android中付款

标题: 将这些标头添加到您的请求中(请注意,应在“入门”部分中将“ your_api_key”替换为服务帐户API密钥)。

标题值 内容类型application / vnd.ni-identity.v1 + json

基本授权:your_api_key

正文/表格数据: 将以下信息添加到您的请求的表单/正文中。

示例请求(正文): JSON格式 { ‘realmName’:‘ni’ }

这些是标题和内容类型,我使用翻新方法创建了一个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);

我收到错误消息,告诉它一个错误的请求,如何解决此问题

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

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

答案 1 :(得分:0)

这对我有用,我将所有标头都放在标头图中

创建地图

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