zxing/zxing · error · IOException

No Location

Error message

No Location

What it means

Thrown by HttpHelper.downloadViaHttp when an HTTP 302 (HTTP_MOVED_TEMP) response is received but the server omitted the 'Location' response header. ZXing follows redirects manually (up to 5 hops) because Android's HttpURLConnection cannot follow redirects across scheme changes (HTTP->HTTPS). A 302 with no Location is a malformed redirect that cannot be followed.

Source

Thrown at android/src/com/google/zxing/client/android/HttpHelper.java:118

      URL url = new URL(uri);
      HttpURLConnection connection = safelyOpenConnection(url);
      connection.setInstanceFollowRedirects(true); // Won't work HTTP -> HTTPS or vice versa
      connection.setRequestProperty("Accept", contentTypes);
      connection.setRequestProperty("Accept-Charset", "utf-8,*");
      connection.setRequestProperty("User-Agent", "ZXing (Android)");
      try {
        int responseCode = safelyConnect(connection);
        switch (responseCode) {
          case HttpURLConnection.HTTP_OK:
            return consume(connection, maxChars);
          case HttpURLConnection.HTTP_MOVED_TEMP:
            String location = connection.getHeaderField("Location");
            if (location != null) {
              uri = location;
              redirects++;
              continue;
            }
            throw new IOException("No Location");
          default:
            throw new IOException("Bad HTTP response: " + responseCode);
        }
      } finally {
        connection.disconnect();
      }
    }
    throw new IOException("Too many redirects");
  }

  private static String getEncoding(URLConnection connection) {
    String contentTypeHeader = connection.getHeaderField("Content-Type");
    if (contentTypeHeader != null) {
      int charsetStart = contentTypeHeader.indexOf("charset=");
      if (charsetStart >= 0) {
        return contentTypeHeader.substring(charsetStart + "charset=".length());
      }
    }

View on GitHub (pinned to 19aa2d8254)

Solutions

  1. Verify the URL resolves correctly in a browser or with curl -I to inspect the redirect chain and Location header.
  2. If you control the server, ensure all 302/301 responses include a valid absolute or relative Location header.
  3. Catch IOException around downloadViaHttp and show a user-friendly message instead of crashing.
  4. Retry the request once after a short delay, as transient server misconfigurations can resolve.

Example fix

// before
CharSequence result = HttpHelper.downloadViaHttp(uri, HttpHelper.ContentType.HTML, 8192);

// after
CharSequence result;
try {
  result = HttpHelper.downloadViaHttp(uri, HttpHelper.ContentType.HTML, 8192);
} catch (IOException e) {
  // Surface a user-friendly message; log the failed URI for debugging
  result = null;
  Log.w(TAG, "Download failed for " + uri + ": " + e.getMessage());
}
Defensive patterns

Strategy: try-catch

Validate before calling

// No reliable pre-check exists for server redirect behavior.
// Validate the URI scheme before the call:
if (uri == null || !(uri.startsWith("http://") || uri.startsWith("https://"))) {
  // Skip download for non-HTTP URIs
  return;
}

Type guard

if (uri instanceof String && (uri.startsWith("http://") || uri.startsWith("https://"))) {
  // safe to call downloadViaHttp
}

Try / catch

try {
  CharSequence content = HttpHelper.downloadViaHttp(uri, type, maxChars);
} catch (IOException e) {
  if ("No Location".equals(e.getMessage())) {
    // Malformed redirect; surface original URI to user
    openInBrowser(uri);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: downloadViaHttp(uri, type, maxChars) receives a response with code 302 but connection.getHeaderField("Location") returns null. This happens with broken URL shorteners (bit.ly, t.co, goo.gl), misconfigured reverse proxies, or servers that send 302 without a Location header.

Common situations: Scanning a barcode containing a shortened or redirected URL whose redirect endpoint is misconfigured. A CDN or load balancer returning 302 without Location during a maintenance window. The domain listed in REDIRECTOR_DOMAINS whose service is temporarily broken.

Related errors


AI-assisted analysis of zxing/zxing@19aa2d8254 (2026-08-14). Data as JSON: /api/errors/de76ddba59e09e0c. Report an issue: GitHub.