gpt4 book ai didi

Android:HttpUrlConnection 对象在连接后返回错误 301

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:31 25 4
gpt4 key购买 nike

我正在尝试使用 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但我不清楚问题的解决方案是什么。

你能帮我找出并解决问题吗?

更新:正如greenapps 在他的评论中指出的那样,连接是通过https 完成的。该评论确定了问题并帮助我修复了代码。

在我的代码中,我用来构建基本 URL 的字符串的协议(protocol)值为 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 页面。你最好直接使用那个 url,因为现在有重定向。

关于Android:HttpUrlConnection 对象在连接后返回错误 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43398845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com