【问题标题】:httpUrlConnection keep returning Not Found in Android 4.4httpUrlConnection 在 Android 4.4 中不断返回 Not Found
【发布时间】:2014-09-02 05:38:28
【问题描述】:

我正在使用一段代码从服务器获取 json 文件,该文件确实存在于服务器上,但 httpUrlConnection 保持返回 responseMessage“未找到” 这是我的代码

private void openHttpUrlConnection(String urlString) throws IOException {
        Log.d("urlstring in parser", urlString + "");
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        httpConnection = (HttpURLConnection) connection;
        httpConnection.setConnectTimeout(30000);
        httpConnection.setRequestMethod("GET");

        httpConnection.connect();
        Log.d("connection",httpConnection.getResponseMessage());
    }

当我切换到 httpClient 时,它对于相同的 url 可以正常工作

private void openHttpClient(String urlString) throws IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams httpParams = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);

    HttpGet httpGet = new HttpGet(urlString);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity httpEntity = httpResponse.getEntity();
    inputStream = httpEntity.getContent();

    reader = new BufferedReader(
            new InputStreamReader(inputStream, "UTF-8"), 8);
}

json文件的url是http://aizariacouncil.org/products.json

【问题讨论】:

标签: android json httpclient httpurlconnection android-4.4-kitkat


【解决方案1】:

您的网络服务器尝试将来自移动设备的请求重定向到另一个网址:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://mobile.aizariacouncil.org/products.json">here</a>.</p>
 <hr>
<address>Apache Server at www.aizariacouncil.org Port 80</address>
</body></html

它尝试重定向的 url 给出 404。你应该修复服务器。

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 2016-03-16
    • 2021-07-10
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多