如何修复500 Bad Request错误? [java,android]

时间:2018-12-01 03:36:02

标签: java android ios swift kotlin

我想将Swift代码转换为Android应用程序的Java代码。我尝试使用“ HttpsURLConnection”,但是它不起作用。

我不确定是否应该使用“ HttpsURLConnection方法”还是应该使用其他方法。

HttpsURLConnection失败。

responseCode: 500 (HttpsURLConnection.getResponseCode())
message2: Bad Request (HttpsURLConnection.getResponseMessage())

如何解决500错误请求错误?

        URL url = new URL("https://***test.herokuapp.com/payment.php");
        HashMap<String, Object> params = new HashMap<>();
        params.put("stripeToken", token);
        params.put("amount", 1500);
        params.put("currency", "jpy");
        params.put("description", "anyone");

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        int responseCode = conn.getResponseCode(); // responseCode: 500

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            Log.d("Success", String.valueOf(responseCode));
        } else {
            String code = String.valueOf(responseCode);
            String message2 = conn.getResponseMessage(); // message2: Bad Request

        }

在Swift中(有效)

let url = "https://***test.herokuapp.com/payment.php"
    let params: [String : Any] = [
        "stripeToken" : token,
        "amount" : totalAmount,
        "currency" : "jpy",
        "description" : "anyone",

    ]

    let manager = AFHTTPSessionManager().post(url, parameters: params, success: { (operation, responseObject) in
        if let response = responseObject as? [String: String] {
            print(response["status"]! + "________" + response["message"]!)
            ProgressHUD.showSuccess("payment success!!")
        }

    }) { (operation, error) in
        if error != nil {
            print("errorMessage \(error.localizedDescription)")
            ProgressHUD.showError(error.localizedDescription)
            return;
        }
    }

1 个答案:

答案 0 :(得分:0)

谢谢您的建议。 我使用“排球”,并且有效。

    final String urlStr = "https://****.php";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, urlStr,

            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(StripeActivity.this,"SuccessToken",Toast.LENGTH_LONG).show();
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("VolleyErrorHere", error.toString());
                }
            }
    ){

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> params = new HashMap<>();

            params.put("stripeToken", token.getId());
            params.put("amount", "120");
            params.put("currency", "jpy");
            params.put("description", "anyone");

            return params;
        }
    }; // end of string Request
相关问题