如何修复Android Java OkHttpClient请求数据架构验证失败?

时间:2020-07-01 06:32:30

标签: java android postman okhttp

我在sharedPreferences上有一个访问令牌,所有其他API都将使用它作为授权来使用其他API。现在几天来,我一直在尝试调试此错误。

{"error":{"statusCode":"400","name":"SchemaValidationError","message":"Request Data Schema Validation fails: ","additionalInfo":{"errors":{"headers.content-type":["The selected headers.content-type is invalid."]}}}}

在邮递员上,我得到的答复是我所期望的:

    "Data": {
        "subscription_id": "db725193-89e9-40ce-b1e5-7c30e0d0fc38",
        "Status": "AwaitingAuthorisation",
        "CreationDateTime": "2020-07-01T06:20:06.164Z",
        "Permissions": [
            "DoPeer2Peer"
        ]
    },
    "Links": {
        "self": "http://localhost:3000/api/open-banking/v1/subscriptions/db725193-89e9-40ce-b1e5-7c30e0d0fc38"
    },
    "Meta": {
        "total-pages": 1
    }
}

这是我的Android Java代码,请协助我,因为错误提示所选的headers.content-type无效。但是为什么我不能像邮递员那样把它作为标题发布呢?

        //Retrieve token wherever necessary
        SharedPreferences preferences = getSharedPreferences(getString(R.string.sharedPreferences_light_token), MODE_PRIVATE);

        String retrivedToken = preferences.getString("TOKEN", null);
        //second parameter default value.
        //PreferenceManager.getDefaultSharedPreferences(context).getString("TOKEN",null);

        Toast.makeText(APIActivity.this, "I have the access token " + retrivedToken, Toast.LENGTH_SHORT).show();
        //Call<ResponseBody> call  = requestLightTokenClient.getLightToken(AccessToken);

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\n \"Data\": {\"Permissions\": [\"DoPeer2Peer\"], \"WalletId\": \"265993\",\"ToWalletId\":\"0785111589\", \"Amount\":\"70\"}\n}    ");
        Request request = new Request.Builder()
                .url("https://MY BASE URL/open-banking/v1/subscriptions")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("x-fapi-financial-id", "OB/2017/001")
                .addHeader("x-ibm-client-id", "MY CLIENT ID")
                .addHeader("x-ibm-client-secret", "MY CLIENT SECRET")
                .addHeader("Authorization", "Bearer " + retrivedToken)
                .build();
        Log.d("TAG", "Subscription call " + request);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {

                e.printStackTrace();
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {

                // For Debugging comment the String
                Log.d("TAG", response.body().string());
                //markertingResponse = response.body().string();
                //AccessToken = response.body().string();
                APIActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //marketingTextView.setText(markertingResponse);

                    }
                });
            }
        });

    }

任何帮助我都会感激的。谢谢

0 个答案:

没有答案