httpUrlConnection继续返回Android 4.4中未找到

时间:2014-09-02 05:38:28

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

我使用一段代码从服务器上获取json文件,该文件确实存在于服务器上,但httpUrlConnection保持返回responseMessage" Not Found" 这是我的代码

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文件网址为http://aizariacouncil.org/products.json

1 个答案:

答案 0 :(得分:0)

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

<!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.你应该修复服务器。