I'm trying to translate a language to another language with Microsoft Text Translator API. I already have this working on java, but when I ported it to Android. It gives me this error. It seems to be pointing me out to IOUtils but I think i already added that prerequisite in the Project Structure. Is there anyway to not use the IOUtils though?. Like OutputStream?. It seems that IOUtils is the problem, but IDK. I can't understand the logcat error. :/
my logcat is here:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ExceptionInInitializerError
at org.apache.commons.io.IOUtils.write(IOUtils.java:2049)
at com.example.joshu.translatorownimplementation.MainActivity.translate(MainActivity.java:101)
at com.example.joshu.translatorownimplementation.MainActivity$LongOperation.doInBackground(MainActivity.java:70)
at com.example.joshu.translatorownimplementation.MainActivity$LongOperation.doInBackground(MainActivity.java:55)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets
at org.apache.commons.io.Charsets.<clinit>(Charsets.java:120)
at org.apache.commons.io.IOUtils.write(IOUtils.java:2049)
at com.example.joshu.translatorownimplementation.MainActivity.translate(MainActivity.java:101)
at com.example.joshu.translatorownimplementation.MainActivity$LongOperation.doInBackground(MainActivity.java:70)
at com.example.joshu.translatorownimplementation.MainActivity$LongOperation.doInBackground(MainActivity.java:55)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Here is my Code:
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MSTranslateAPI {
private static String output;
private static String key = "<MS KEY for Translator API>";
public static void main(String[] args) throws Exception {
// TODO: Specify your translation requirements here:
String fromLang = "en";
String toLang = "ko";
String text = "Hello Friend";
MSTranslateAPI.translate(fromLang, toLang, text);
}
public static void translate(String fromLang, String toLang, String text) throws Exception {
String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection();
authConn.setDoOutput(true);
authConn.setRequestMethod("POST");
authConn.setRequestProperty("Ocp-Apim-Subscription-Key", key);
IOUtils.write("", authConn.getOutputStream(), "UTF-8");
String token = IOUtils.toString(authConn.getInputStream(), "UTF-8");
authConn.disconnect();
System.out.println("TOKEN: "+token);
if(authConn.getResponseCode()==200) {
String appId = URLEncoder.encode("Bearer " + token, "UTF-8");
String text2 = URLEncoder.encode(text, "UTF-8");
String from = fromLang;
String to = toLang;
String translatorTextApiUrl ="https://api.microsofttranslator.com/v2/http.svc/GetTranslations?appid="+appId+"&text="+text2+"&from="+from+"&to="+to+"&maxTranslations=5";
HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection();
translateConn.setRequestMethod("POST");
translateConn.setDoOutput(true);
IOUtils.write("", translateConn.getOutputStream(), "UTF-8");
String resp = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
translateConn.disconnect();
System.out.println(resp+"\n\n");
}
else {
}
}
}