我正在尝试使用Microsoft Text Translator API将语言翻译成另一种语言。我已经将这个工作用于java,但是当我将它移植到Android时。它给了我这个错误。它似乎指向了IOUtils,但我想我已经在项目结构中添加了先决条件。反正有没有使用IOUtils?像OutputStream?似乎IOUtils是问题,但IDK。我无法理解logcat错误。 :/
我的logcat在这里:
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)
这是我的代码:
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 {
}
}
}
答案 0 :(得分:1)
logcat消息中的堆栈跟踪表明StandardCharsets类不可用。根据{{3}},该类在android JDK中,但需要API版本19或更高版本。如果你想继续使用IOUtils,你可以升级你定位的android版本。
答案 1 :(得分:1)
顺便说一句。我的应用程序没有崩溃,但它只是不处理IOUtils任务,即使我已经使用上面的API 21(我教过已经工作,因为在编译期间,它没有给出特定的错误,除了那个logcat我不能阅读/理解,如果不是上面接受的答案)。 IOUtils似乎与android完全不兼容?因为在我的应用程序中,就我所关心的那样,即使这些依赖项/ jar文件已经在我的java版本中工作,也只是继续使用该logcat错误/根本没有输出,即使有适当的依赖项导入。所以我所做的就是我完全摆脱了IOUtils,并通过手动硬编码自己的方法将任务分配给它,结果证明它工作得很好,没有更多的IOUtils导入好了。事实证明导入IOUtils的困难是不值得的,你最好只是硬编码自己所做的事情,这在我的案例中无论如何都是非常基本的。或者至少就我而言,我以一种我可以完全取代它并专门为我的应用程序工作的方式对IOUtils代码部分进行了硬编码。