Whenever I try to an API request from the: "http://api.openweathermap.org/data/2.5/weather?q=London"
the code fails to fetch the data.
However, this is working fine with other URLs. Here's the Main java file.
I've taken added the necessary permissions in and manifest file.
package thebestone.vicky.jsondemo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
class GetData extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try{
url = new URL(strings[0]);
urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while(data != -1){
char ch = (char) data;
result = result + ch;
data = reader.read();
}
return result;
}catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("DATA FETCHED: ", s);
}
}
public void onClick(View view){
GetData task = new GetData();
task.execute("http://api.openweathermap.org/data/2.5/weather?q=London");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
logcat:
08-18 19:05:10.478 2380-4307/thebestone.vicky.jsondemo W/System.err: java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/weather?q=London
08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo W/System.err: at thebestone.vicky.jsondemo.MainActivity$GetData.doInBackground(MainActivity.java:33)
08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo W/System.err: at thebestone.vicky.jsondemo.MainActivity$GetData.doInBackground(MainActivity.java:19)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.lang.Thread.run(Thread.java:761)
08-18 19:05:10.481 2380-2380/thebestone.vicky.jsondemo I/DATA FETCHED:: Failed
Also I'm not able to figure out the error in the logcat and program.
Any help would be appreciated.
I suppose you have got the same 401 Unauthorized as me. If im not mistaken the answer from @Raghunandan should be your solution.