【发布时间】:2017-09-09 23:08:37
【问题描述】:
我正在尝试使用 URL 连接到 Web 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 的情况下,连接从提供的日志中返回状态码 301。我还记录了生成的 URL,我从 logcat 复制了它并在我的浏览器中尝试了它它运作良好。这是构建的 URL: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的评论,我只是把字符串中的protocol部分改成了https,所以变成了:
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query";
这解决了问题。
谢谢。
【问题讨论】:
-
如果您点击此处的 http 链接,您将看到浏览器显示一个 https 页面。您最好直接使用该网址,因为现在有重定向。
-
@greenapps 感谢您的评论!您的评论帮助我找到了解决方案。正如您所说它使用的是 https,我检查了用于构建初始 URL 的基本字符串,发现我使用的是 http,我只是将其修改为 https,并且效果很好。非常感谢。如果您愿意,可以将其发布为答案,然后我会接受。
标签: android redirect httpurlconnection asynctaskloader