You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public static String translate(@NonNull String from, @NonNull String to, @NonNull String request) {
if (from == null) {
throw new NullPointerException("from is marked non-null but is null");
} else if (to == null) {
throw new NullPointerException("to is marked non-null but is null");
} else if (request == null) {
throw new NullPointerException("request is marked non-null but is null");
} else {
try {
URL url = new URL(urlApi);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("accept", "application/json");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write("q=" + URLEncoder.encode(request, "UTF-8") + "&source=" + from + "&target=" + to + "&format=text");
writer.flush();
writer.close();
httpConn.getOutputStream().close();
if (httpConn.getResponseCode() / 100 != 2) {
throw new BadTranslatorResponseException(httpConn.getResponseCode(), urlApi);
} else {
InputStream responseStream = httpConn.getInputStream();
Scanner s = (new Scanner(responseStream)).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
return ((TranslateResponse)JsonUtil.from(response, TranslateResponse.class)).getTranslatedText();
}
} catch (Exception var9) {
var9.printStackTrace();
throw new RuntimeException(var9);
}
}
}
public static String translate(@NonNull Language from, @NonNull Language to, @NonNull String request) {
if (from == null) {
throw new NullPointerException("from is marked non-null but is null");
} else if (to == null) {
throw new NullPointerException("to is marked non-null but is null");
} else if (request == null) {
throw new NullPointerException("request is marked non-null but is null");
} else {
return to != Language.NONE && from != to ? translate(from.getCode(), to.getCode(), request) : request;
}
}
public static String translate(@NonNull Language to, @NonNull String request) {
if (to == null) {
throw new NullPointerException("to is marked non-null but is null");
} else if (request == null) {
throw new NullPointerException("request is marked non-null but is null");
} else {
return to == Language.NONE ? request : translate("auto", to.getCode(), request);
}
}
private Translator() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
public static void setUrlApi(String urlApi) {
Translator.urlApi = urlApi;
}
}
The text was updated successfully, but these errors were encountered:
When I call the API in Java, it tip 403
java program as follows
public final class Translator {
private static String urlApi = "https://translate.fedilab.app/translate";
}
The text was updated successfully, but these errors were encountered: