Android: HttpUrlConnection 对象 returns 连接后错误 301
Android: HttpUrlConnection object returns error 301 after connection
我正在尝试使用 URL 连接到网络 API。但是,我从服务器(永久移动) 收到 301 错误,尽管提供的 URL 在我的浏览器中尝试时运行良好且没有错误。
这是构建 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的情况下,我可以从提供的日志中知道连接returns状态码为301。我也记录了生成的URL,我复制了它来自 logcat,并在我的浏览器中尝试过,效果很好。这是内置的URL:http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=10&minmag=6&orderby=magnitude
我检查了这个问题: 但我不清楚问题的解决方案是什么。
你能帮我找出并解决问题吗?
UPDATE:正如 greenapps 在他的评论中指出的那样,连接是通过 https 完成的.该评论确定了问题并帮助我修复了代码。
在我的代码中,我用来构建基本 URL 的字符串的协议值为 http 而不是 https,它是:
private static final String USGS_REQUEST_URL =
"http://earthquake.usgs.gov/fdsnws/event/1/query";
看了greenapps的评论,我只是把字符串中的protocol部分改成了https,所以变成了:
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query";
问题解决了。
谢谢。
如果您单击此处的 http link,您将看到浏览器显示 https 页面。您最好直接使用 url,因为现在有重定向。
这是因为地址http转https了。
为了避免这种情况,您需要将请求地址转换为https。
我正在尝试使用 URL 连接到网络 API。但是,我从服务器(永久移动) 收到 301 错误,尽管提供的 URL 在我的浏览器中尝试时运行良好且没有错误。
这是构建 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的情况下,我可以从提供的日志中知道连接returns状态码为301。我也记录了生成的URL,我复制了它来自 logcat,并在我的浏览器中尝试过,效果很好。这是内置的URL:http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=10&minmag=6&orderby=magnitude
我检查了这个问题:
你能帮我找出并解决问题吗?
UPDATE:正如 greenapps 在他的评论中指出的那样,连接是通过 https 完成的.该评论确定了问题并帮助我修复了代码。
在我的代码中,我用来构建基本 URL 的字符串的协议值为 http 而不是 https,它是:
private static final String USGS_REQUEST_URL =
"http://earthquake.usgs.gov/fdsnws/event/1/query";
看了greenapps的评论,我只是把字符串中的protocol部分改成了https,所以变成了:
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query";
问题解决了。
谢谢。
如果您单击此处的 http link,您将看到浏览器显示 https 页面。您最好直接使用 url,因为现在有重定向。
这是因为地址http转https了。 为了避免这种情况,您需要将请求地址转换为https。