我試圖使用URL連接到Web API。然而,我從服務器(永久移動)得到了一個301錯誤,雖然當我在瀏覽器中嘗試它時,提供的URL運行良好,沒有錯誤。Android:HttpUrlConnection對象在連接後返回301錯誤
這裏是構建URL代碼:
public Loader<List<Earthquake>> onCreateLoader(int i, Bundle bundle) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String minMagnitude = sharedPrefs.getString(
getString(R.string.settings_min_magnitude_key),
getString(R.string.settings_min_magnitude_default));
String orderBy = sharedPrefs.getString(
getString(R.string.settings_order_by_key),
getString(R.string.settings_order_by_default)
);
Uri baseUri = Uri.parse(USGS_REQUEST_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("format", "geojson");
uriBuilder.appendQueryParameter("limit", "10");
uriBuilder.appendQueryParameter("minmag", minMagnitude);
uriBuilder.appendQueryParameter("orderby", orderBy);
Log.i ("the uri is ", uriBuilder.toString());
return new EarthquakeLoader(this, uriBuilder.toString());
}
下面是嘗試連接到由URL代表的資源代碼:
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
Log.i("The received url is " , url +"");
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); //this log returns 301
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// Closing the input stream could throw an IOException, which is why
// the makeHttpRequest(URL url) method signature specifies than an IOException
// could be thrown.
inputStream.close();
}
}
return jsonResponse;
}
我能知道連接返回在狀態碼不是200的情況下提供的日誌中的狀態代碼爲301.我還記錄了生成的URL,我從logcat中複製了它,並在我的瀏覽器中嘗試了它,並且它運行良好。這裏是內置的網址:http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=10&minmag=6&orderby=magnitude
我檢查了這個問題:Android HttpURLConnection receives HTTP 301 response code但我不清楚這個問題的解決方案是什麼。
你能幫我找出並解決問題嗎?
UPDATE:作爲greenapps在他的評論中指出的,連接是通過HTTPS 做。該評論確定了問題並幫助我修復了代碼。
在我的代碼,我用來建立基本的URL字符串,有協議的價值爲http沒有使用https,它是:
private static final String USGS_REQUEST_URL =
"http://earthquake.usgs.gov/fdsnws/event/1/query";
閱讀greenapps評論後,我剛換了協議部分在到https的字符串中,所以它變成:
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query";
這就解決了這個問題。
謝謝。
如果你在這裏使用http鏈接,你會看到瀏覽器顯示一個https頁面。您最好直接使用該URL,因爲現在有重定向。 – greenapps
@greenapps感謝您的評論!您的評論幫助我找出解決方案。正如你所說,它使用https,我檢查了我用來構建初始URL的基本字符串,我發現我使用的是http,我只是將其修改爲https,並且它工作得很好。非常感謝。如果您願意,您可以將其作爲答案發布,然後我會接受它。 – Dania