Solution 1 :
Have you given android studio permission to use the INTERNET in the AndroidManifest.xml?
Solution 2 :
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.
Problem :
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");
}
}
Comments
Comment posted by Yusef Maali
Have you tried to open the url in a browser? I’ve tried with postman and it works. Maybe is there a problem in your local network?
Comment posted by Saliko
Yeah I tried and I get response
Comment posted by Yusef Maali
an http 301 means the url you are calling is “old” and has been moved to another url. This means you should follow the new url, that is passed through the headers in the response. Try to read the http headers and look for more information.
Comment posted by Saliko
@Yusef Maali, i tried a different link and I got response.I dont know why ):
Comment posted by Yusef Maali
please, check the answers, you’ll find all the details
Comment posted by Saliko
Yes I did, but i still can’t fetch data
Comment posted by Mahmoud Omara
why aren’t you using retrofit or a similar library for the network call? it seems redundant to use an HTTP connection now unless it’s specifically needed
Comment posted by Saliko
Maybe I’ll check on that later