我尝试使用网址连接到网络API。但是,我从服务器(永久移动)收到 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但我不清楚问题的解决方案是什么。
你能帮我识别并解决问题吗?
更新:在评论中显示 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";
解决了这个问题。
感谢。
答案 0 :(得分:3)
如果您在此处单击您的http链接,您将看到浏览器显示https页面。您最好直接使用该URL,因为现在有重定向。
答案 1 :(得分:0)
这是因为地址http转移到https。 为避免这种情况,您需要将请求地址转换为https。