Reputation: 143
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 {
}
}
}
Upvotes: 1
Views: 310
Reputation: 143
BTW. My app doesn't crash, but it just doesn't process the IOUtils task I have even if I already used API 21 above (which I taught is already working because during compile, it doesn't give a specific error except on that logcat that I can't read/understand if not for the accepted answer above). IOUtils seems to be totally incompatible to android at all?. Because in my app, just kept on going with that logcat error / no output at all even with proper dependency imports as far as I'm concerned(even though those dependencies/jar files are already working in my java version of it). So what I did is that I totally got rid of the IOUtils and made the task assigned to it by manual hard coding myself its methods and it turns out it worked great with no more IOUtils import alright. Turns out the hardship of importing the IOUtils is not worth it, and you better just hard code yourself what it does which turns out to be very basic anyway in my case. Or at least in my part, I hard coded the IOUtils code part in a way that I could totally replace it and work specifically for my app.
Upvotes: 1
Reputation: 639
The stacktrace in the logcat message indicates the StandardCharsets class is not available. According to https://developer.android.com/reference/java/nio/charset/StandardCharsets.html, the class is in the android JDK, but requires API version 19 or higher. You could upgrade the android version you target if you want to keep your use of IOUtils.
Upvotes: 1