I'm trying to access some json data, from a website. It has basic authentication, so i can't access the data and receive the exception: Filenotfoundexception. I now i'm in the right path, but i need help for the last steps. My code is:
private class jsonConnection extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String username = "---";
String password = "---";
String credentials = username + ":" + password;
String sensorID = "sensors";
String api = "api";
String webURL = "https://" + credentials + "@---";
String credBase64 = Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT).replace("\n", "");
try {
URL url = new URL(webURL + "/" + sensorID + "/" + api);
System.out.println(url);
System.out.println(credentials);
connection = (HttpsURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setReadTimeout(30000);
connection.setConnectTimeout(30000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.addRequestProperty("HTTP_Authorization", "basic" + credentials);
connection.connect();
InputStream stream = connection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(stream));
//StringBuffer buffer = new StringBuffer();
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data = data + line;
}
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("coord", coord);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
i know this code is enough to establish a connection to any other JSON data, but since there's basic authentication i cant seem to connect to this website's json. I appreciate the help beforehand!