Saliko
Saliko

Reputation: 164

OpenWeatherMap REST API is returning always HTTP 301

I'm trying to process JSON data from an API (http://samples.openweathermap.org/data/2.5/weather?q=London,uk) but instead I get this on my logcat window: 301 Moved Permanently

Here is my Class:

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");
    }
}

Upvotes: 2

Views: 853

Answers (2)

Yusef Maali
Yusef Maali

Reputation: 2431

I've used Postman (give it a try) to analyze what happens.

The url you provided is: http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02.
Reading the response, this is what you will found:

Body:

<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>

Raw HTML response:

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

As said in the comments, when you get an HTTP 30x (see: Redirection messages) the server is telling you the url you are calling is old. If you want a correct response you should follow (so the "redirect" message) the new url the server is passing you in the http headers.

The http header you are looking for is Location, that is reporting this url:

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

Going through a little diff with the two urls, the server is telling you to call the https flavor of the url.
This is a common practice, please use always https urls if available.

Upvotes: 0

edwin
edwin

Reputation: 65

Have you given android studio permission to use the INTERNET in the AndroidManifest.xml?

Upvotes: 1

Related Questions