HttpUrlConnection игнорирует введенные значения - PullRequest
0 голосов
/ 11 января 2019

Я пытаюсь сделать HttpUrlConnection POST для токена песочницы PayPal, чтобы получить токен доступа, но когда я запускаю свое приложение, он игнорирует введенные значения (метод, doOutput ...) , Я тоже пробовал HttpsUrlConnection. В чем проблема?

Этот метод вызывается из класса AsyncTask внутри метода doInBackground().

private String makeHttpAuth(URL authURL) throws IOException, JSONException {
// authUrl = "https://api.sandbox.paypal.com/v1/oauth2/token"
// BASICINPUT = "Basic " + Base64.encodeToString(AUTHINPUT.getBytes(), Base64.NO_WRAP);

String authJsonResponse = "";
String grant = authBody().toString();

    if (authURL == null) {
        return authJsonResponse;
    }
    HttpURLConnection authentication = null;
    InputStream inputStream = null;

    try {

        authentication = (HttpURLConnection) authURL.openConnection();
        authentication.setRequestMethod("POST");
        authentication.setConnectTimeout(15000);
        authentication.setRequestProperty(AUTHORIZE, BASICINPUT);
        authentication.setDoInput(true);
        authentication.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(authentication.getOutputStream());

        writer.write(grant);
        writer.flush();
        writer.close();
        authentication.connect();

        if (authentication.getResponseCode() == 200) {
            inputStream = authentication.getInputStream();
            authJsonResponse = readFromStream(inputStream);
        } else {
            Log.e(LOG_TAG, "Error response code: " + authentication.getResponseCode());
        }

    } finally {
        if (authentication != null) {
            authentication.disconnect();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
    return authJsonResponse;

Ожидаемая:

...

  • метод: " POST "
  • doOutput: true
  • responseCode: 200

...

Фактический:

...

  • метод: " GET "
  • doOutput: false
  • responseCode: -1

...

...