Java OpenWeatherMapRESTAPI始终返回HTTP 301

Java OpenWeatherMapRESTAPI始终返回HTTP 301,java,android,android-asynctask,httpurlconnection,openweathermap,Java,Android,Android Asynctask,Httpurlconnection,Openweathermap,我正在尝试处理来自API的JSON数据(http://samples.openweathermap.org/data/2.5/weather?q=London,英国),但我在我的logcat窗口中看到了这一点:301永久移动 这是我的班级: public类MainActivity扩展了AppCompatActivity{ 公共类DownloadTask扩展了AsyncTask{ @凌驾 受保护的字符串doInBackground(字符串…URL){ 字符串结果=”; 网址; HttpURLCon

我正在尝试处理来自API的JSON数据(
http://samples.openweathermap.org/data/2.5/weather?q=London,英国
),但我在我的logcat窗口中看到了这一点:
301永久移动

这是我的班级:

public类MainActivity扩展了AppCompatActivity{
公共类DownloadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串结果=”;
网址;
HttpURLConnection-urlConnection;
试一试{
url=新url(url[0]);
urlConnection=(HttpURLConnection)url.openConnection();
InputStream in=urlConnection.getInputStream();
InputStreamReader reader=新的InputStreamReader(in);
int data=reader.read();
while(数据!=-1){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
返回结果;
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
Log.i(“JSON”,s);
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask任务=新建DownloadTask();
任务。执行(“http://samples.openweathermap.org/data/2.5/weather?q=London,英国&appid=439d4b804bc8187953eb36d2a8c26a02”);
}
}

您是否已授予android studio在AndroidManifest.xml中使用INTERNET的权限?

我已使用(尝试一下)来分析发生了什么

您提供的url是:
http://samples.openweathermap.org/data/2.5/weather?q=London,英国&appid=439d4b804bc8187953eb36d2a8c26a02

阅读回复,您会发现:

正文:

<html>

<head>
    <title>301 Moved Permanently</title>
</head>

<body bgcolor="white">
    <center>
        <h1>301 Moved Permanently</h1>
    </center>
    <hr>
    <center>openresty/1.9.7.1</center>
</body>

</html>
正如在评论中所说,当您收到
http30x
(请参阅:)时,服务器会告诉您所调用的url是旧的。如果您想要一个正确的响应,您应该遵循服务器在http头中传递给您的新url(因此“重定向”消息)

您要查找的http头是
位置
,它报告此url:

https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02
这两个url有点不同,服务器告诉您调用url的
https
风格。

这是一种常见做法,如果可用,请始终使用
https
URL。

是的,但我仍然无法获取数据。为什么您不为网络调用使用改装或类似的库?除非特别需要,否则现在使用HTTP连接似乎是多余的。也许我以后会检查一下。您是否尝试在浏览器中打开url?我和邮递员试过了,效果不错。也许你的本地网络有问题吗?是的,我试过了,我得到了响应。http 301表示你正在调用的url是“旧的”,已经被移动到另一个url。这意味着您应该遵循通过响应中的标题传递的新url。尝试阅读http标题并查找更多信息。@Yusef Maali,我尝试了另一个链接,但得到了响应。我不知道为什么):请检查答案,您会找到所有详细信息
https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02