I would ultimately like to make a HTTP request to the NCBI Entrez database to retrieve some accession numbers. I'm starting out small here, so I wanted to make a functional HTTP request, regardless of the endpoint. But no matter the endpoint I use, a Unknown Host Exception is thrown. Why?
I have included all of my code, but the problem only happens in the method getEntireSubject
.
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Subject {
private String id;
private String sequence;
// Position on ref sequence where alignment with query begins, inclusive
private int start;
// Position on ref sequence where alignment with query ends, inclusive
private int end;
public Subject(String accessNum, int hitFrom, int hitTo, String seq) {
id = accessNum;
sequence = seq;
start = hitFrom;
end = hitTo;
getEntireSubject();
}
// Getters
public String getSequence() {
return sequence;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
// Fetches accession number from NCBI
private void getEntireSubject() {
try {
String link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/einfo.fcgi";
link = "https://jsonplaceholder.typicode.com/posts/1";
InputStream response = new URL(link).openStream();
try (Scanner scanner = new Scanner(response)) {
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
} catch (Exception e) {
System.out.println(e);
}
}
}