亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要凌空授權

需要凌空授權

阿晨1998 2022-08-17 10:14:09
我正在嘗試轉換 Unirest   HttpResponse<String> response = Unirest.post("https://api.tap.company/v2/charges")  .header("authorization", "Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ")  .header("content-type", "application/json")  .body("{\"amount\":1,\"currency\":\"KWD\",\"receipt\":{\"email\":false,\"sms\":true},\"customer\":{\"first_name\":\"test\",\"phone\":{\"country_code\":\"965\",\"number\":\"50000000\"}},\"source\":{\"id\":\"src_kw.knet\"},\"redirect\":{\"url\":\"http://your_website.com/redirect_url\"}}")  .asString();到凌空RequestQueue queue = Volley.newRequestQueue(this);        String url = "https://api.tap.company/v2/charges";        StringRequest TapREQUEST = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {            @Override public void onResponse(String response) {                Log.w("OnResponse:", response);            }        }, new Response.ErrorListener() {            @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); }        }) {            @Override public Map<String, String> getHeaders() {                Map<String,String> headers = new HashMap<>();                headers.put("content-type", "application/json");                headers.put("authorization", "Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ");                //String auth = "Bearer " + Base64.encodeToString("sk_test_XKokBfNWv6FIYuTMg5sLPjhJ".getBytes(), Base64.DEFAULT);                //headers.put("authorization", auth);                return headers;                    }        };        queue.add(TapREQUEST);但是我得到E /Volley:[396]BasicNetwork.performRequest:意外響應代碼400用于 https://api.tap.company/v2/charges當我點擊鏈接時,我得到{“errors”:[{“code”:“2107”,“description”:“Authorization Required”}]}
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

您必須以不同的方式設置正文參數。讓我們創建返回正確字符串的方法:


@NotNull

private JSONObject getJsonObject() {

    JSONObject params = new JSONObject();

    try {

        params.put("amount", "1");

        params.put("currency", "KWD");


        JSONObject receipt = new JSONObject();

        receipt.put("email", "false");

        receipt.put("sms", "true");

        params.put("receipt", receipt);


        JSONObject customer = new JSONObject();

        customer.put("first_name", "test");

        JSONObject phone = new JSONObject();

        phone.put("country_code", "965");

        phone.put("number", "50000000");

        customer.put("phone", phone);

        params.put("customer", customer);


        JSONObject id = new JSONObject();

        id.put("id", "src_kw.knet");

        params.put("source", id);


        JSONObject url = new JSONObject();

        url.put("url", "http://ib7ar.com");


        params.put("redirect", url);

    } catch (JSONException e) {

        e.printStackTrace();

    }


    return params;

}

現在,您不需要使用方法:getParams()getBody()


@Override

public byte[] getBody() {

    try {

        return getJsonObject().toString().getBytes("utf-8");

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

        return null;

    }

}


查看完整回答
反對 回復 2022-08-17
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號