Hej,
łączę się po API do justjoin.it i otrzymuje jeden wielki string ze wszystkimi ofertami pracy dla hasla "Business Analyst"
.
Mój kod to:
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.Collator;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.Collections.reverseOrder;
import static java.util.function.UnaryOperator.identity;
import static java.util.stream.Collectors.*;
public class Main {
public static void main(String[]args) {
String inline;
String apiUrl = "https://justjoin.it/api/offers/search?keywords[]=Data%20Scientist";
inline = getUrlResponse(apiUrl);
//Using the JSON simple library parse the string into a json object
JSONParser parser = new JSONParser();
JSONArray arr = new JSONArray();
try {
arr = (JSONArray) parser.parse(inline);
} catch (org.json.simple.parser.ParseException e) {
throw new RuntimeException(e);
}
JSONObject obj = new JSONObject();
String linkApi;
String urlToGet;
String urlDescription;
String htmlText;
ArrayList<String> AllJobOffers = new ArrayList<String>();
for (int i = 0; i < arr.size(); i++) {
obj = (JSONObject) arr.get(i);
linkApi = obj.get("id").toString();
urlToGet = "https://justjoin.it/api/offers/" + linkApi;
urlDescription = getUrlResponse(urlToGet);
parser = new JSONParser();
JSONObject arrDescription = new JSONObject();
try {
arrDescription = (JSONObject) parser.parse(urlDescription);
} catch (org.json.simple.parser.ParseException e) {
throw new RuntimeException(e);
}
htmlText = arrDescription.get("body").toString();
// System.out.println(htmlText);
htmlText = htmlText.replaceAll("\\<[^>]*>","");
// System.out.println(htmlText);
AllJobOffers.add(htmlText);
}
String AllWords = String.join("", AllJobOffers);
List<String> words = Arrays.asList(AllWords);
Map<String, Long> collect = words.stream()
.map(String::toLowerCase) // convert to lower case
.collect( // group and count by name
Collectors.groupingBy(Function.identity(), Collectors.counting()));
collect.keySet().stream()
.sorted( // order by count descending, then by name
Comparator
.comparing(collect::get)
.reversed()
.thenComparing(Collator.getInstance()))
.map(k -> k + " (" + collect.get(k) + ")") // map to name and count string
.limit(7) // only first 7 entries
.forEach(System.out::println); // output
}
private static String getUrlResponse(String url){
URL urlAPI = null;
try {
urlAPI = new URL(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
HttpURLConnection conn;
try {
urlAPI = new URL(url);
conn = (HttpURLConnection) urlAPI.openConnection();
conn.setRequestMethod("GET");
conn.connect();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (ProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
int responsecode;
try {
responsecode = conn.getResponseCode();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (responsecode != 200) {
throw new RuntimeException("HttpResponseCode: " + responsecode);
} else {
String inline = "";
Scanner scanner = null;
try {
scanner = new Scanner(urlAPI.openStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
//Write all the JSON data into a string using a scanner
while (scanner.hasNext()) {
inline += scanner.nextLine();
}
scanner.close();
return inline;
}
}
}
-
Proszę o best practices jeśli chodzi o ten kod - co mógłbym napisać lepiej?
-
Na końcu próbuję zrobić listę słów w javie i wyrzucić je do konsoli, ale nie wychodzi mi wynika tak jak tutaj:
Jak mogę zrobić taką listę w javie?
Pozdrowienia i dziękuję,
Jacek