【问题标题】:OpenWeatherMap REST API is returning always HTTP 301OpenWeatherMap REST API 总是返回 HTTP 301
【发布时间】:2020-09-26 06:40:38
【问题描述】:

我正在尝试处理来自 API (http://samples.openweathermap.org/data/2.5/weather?q=London,uk) 的 JSON 数据,但我在我的 logcat 窗口中得到了这个:301 Moved Permanently

这是我的班级:

public class MainActivity extends AppCompatActivity {

    public class DownloadTask extends AsyncTask<String,Void,String> {

        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection urlConnection;
            try {
                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();

                while (data != -1) {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }

                return result;


            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i("JSON",s);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask task = new DownloadTask();
        task.execute("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02");
    }
}

【问题讨论】:

  • 您是否尝试在浏览器中打开网址?我已经尝试过使用邮递员并且它有效。也许您的本地网络有问题?
  • 是的,我试过了,我得到了回应
  • 一个 http 301 表示您正在调用的 url 是“旧的”并且已移动到​​另一个 url。这意味着您应该遵循通过响应中的标头传递的新 url。尝试阅读 http 标头并查找更多信息。
  • @Yusef Maali,我尝试了不同的链接并得到了回复。我不知道为什么):
  • 请检查答案,您会找到所有详细信息

标签: java android android-asynctask httpurlconnection openweathermap


【解决方案1】:

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

【讨论】:

  • 是的,但我仍然无法获取数据
  • 你为什么不使用改造或类似的库来进行网络调用?除非特别需要,否则现在使用 HTTP 连接似乎是多余的
  • 也许我稍后会检查一下
【解决方案2】:

我用Postman(试一试)来分析发生了什么。

您提供的网址是:http://samples.openweathermap.org/data/2.5/weather?q=London,uk&amp;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>

原始 HTML 响应:

HTTP/1.1 301 Moved Permanently
Server: openresty/1.9.7.1
Date: Sun, 07 Jun 2020 10:49:57 GMT
Content-Type: text/html
Content-Length: 190
Connection: keep-alive
Location: https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02

正如 cmets 中所说,当您收到 HTTP 30x(请参阅:Redirection messages)时,服务器会告诉您您调用的 url 是旧的。如果您想要正确的响应,您应该遵循(因此“重定向”消息)服务器在 http 标头中传递给您的新 url。

您要查找的 http 标头是 Location,即报告此 url:

https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02

通过两个网址的一点差异,服务器告诉您调用网址的https 风格。
这是一种常见做法,请始终使用https 网址(如果有)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多