` //url of api service
String data = "https://api.companieshouse.gov.uk/search/companies?q=Apple";
try {
HttpsURLConnection conn = null;
URL url = new URL(data);
conn = (HttpsURLConnection) url.openConnection();
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, mContext.getString(R.string.api_key_companies_house));
conn.setReadTimeout(10000 /* milliseconds */ );
conn.setConnectTimeout(15000 /* milliseconds */ );
conn.setDoOutput(true);
conn.setRequestMethod("GET");
//Log.i("responseCode1", String.valueOf(conn.getResponseCode()));
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
Log.i("responseCode2", String.valueOf(conn.getResponseCode()));
wr.flush();
wr.close();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
// Read Server Response
while((line = reader.readLine()) != null) {
sb.append(line);
break;
}
reader.close();
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch(MalformedURLException e){
e.printStackTrace();
}catch (ProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return data;
}`
I get a 404 response code. This is written in Java for an Android device. The authorisation is working now, but I am not receiving any data back from the server.