Wie bekomme ich 401 Antwort ohne es mit Try / Catch in Android zu behandeln

ich benutzeHttpUrlConnection Netzwerkanfragen von meiner Android-Anwendung zu machen. Alles funktioniert gut, bis auf eine Sache, 401. Immer wenn der Server eine Antwort mit dem Statuscode 401 zurückgibt, wirft meine AppIOException mit einer mitteilung,"no authentication challenge found". Nachdem ich es gegoogelt habe, habe ich keine einzige Lösung gefunden, sondern nur eine Problemumgehung (Behandlung mit try / catch, vorausgesetzt, es handelt sich um eine 401-Antwort).

Hier ist das Code-Snippet:

public Bundle request(String action, Bundle params, String cookie) throws FileNotFoundException, MalformedURLException, SocketTimeoutException,
        IOException {

    OutputStream os;

    String url = baseUrl + action;
    Log.d(TAG, url);
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(30 * 1000);
    conn.setReadTimeout(30 * 1000);
    conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + "AndroidNative");
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    if (cookie != null) {
        conn.setRequestProperty("Cookie", cookie);
    }

    if (params != null) {
        os = conn.getOutputStream();
        os.write(("--" + boundary + endLine).getBytes());
        os.write((encodePostBody(params, boundary)).getBytes());
        os.write((endLine + "--" + boundary + endLine).getBytes());
        uploadFile(params, os);
        os.flush();
        os.close();
    }

    conn.connect();

    Bundle response = new Bundle();
    try {
        response.putInt("response_code", conn.getResponseCode());
        Log.d(TAG, conn.getResponseCode() + "");
        response.putString("json_response", read(conn.getInputStream()));
        List<String> responseCookie = conn.getHeaderFields().get("set-cookie");
        // Log.d(TAG, responseCookie.get(responseCookie.size() - 1));
        response.putString("cookie", responseCookie.get(responseCookie.size() - 1));
    } catch (SocketTimeoutException e) {
        throw new SocketTimeoutException(e.getLocalizedMessage());
    } catch (FileNotFoundException e) {
        throw new FileNotFoundException(e.getLocalizedMessage());
    } catch (IOException e) {
        e.printStackTrace();
        response.putInt("response_code", HttpURLConnection.HTTP_UNAUTHORIZED);
        response.putString("json_response", read(conn.getErrorStream()));
    }

    // debug
    Map<String, List<String>> map = conn.getHeaderFields();
    for (String key : map.keySet()) {
        List<String> values = map.get(key);
        for (String value : values) {
            Log.d(key, value);
        }
    }

    conn.disconnect();

    return response;
}

Ich möchte wirklich wissen, warum diese Ausnahme geworfen wird? Was bedeutet Authentifizierungsaufforderung? Wie wird eine Authentifizierungsaufforderung bereitgestellt? Welche Änderung muss ich in meinem Code vornehmen, um diese Situation zu überwinden?

Bitte erleuchte mich.. :)

Antworten auf die Frage(2)

Ihre Antwort auf die Frage