我对 Android 开发相当陌生,并且正在制作一个使用 Google 翻译 API 翻译内容的程序。错误如下:'令牌语法错误,构造错误。谢谢你。这是我的Java文件:
package org.example.translate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import java.io.BufferedReader;
public class TranslateTask implements Runnable {
private static final String TAG = "TranslateTask";
private final Translate translate;
private final String original, from, to;
TranslateTask(Translate translate, String original, String from, String to) {
this.translate = translate;
this.original = original;
this.from = from;
this.to = to;
}
public void run() {
String trans = doTranslate(original, from, to);
translate.setTranslated(trans);
String retrans = doTranslate(trans, from, to);
translate.setRetranslated(retrans);
}
private String doTranslate (String original, String from, String to) {
String result = translate.getResources().getString(
R.string.translation_error);
HttpURLConnection con = null;
Log.d(TAG, "dotranslate(" + original + ", " + from + ", " + to + ")");
//*This is where the error occours*// try {
if (Thread.interrupted())
throw new InterruptedException();
// Build RESTful query for Google API
String q = URLEncoder.encode(original, "utf-8");
URL url = new URL(
"http://ajax.googleapis.com/ajax/services/language/translate"
+ "?v=1.0" + "&q=" + q + "&langpair=" + from
+ "%7C" + to);
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.addRequestProperty("referer",
"http://www.pragprog.com/titles/eband3/hello-android");
con.setDoInput(true);
con.connect();
if (Thread.interrupted())
throw new InterruptedException();
BufferedReader reader = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"));
String payload = reader.readLine();
reader.close();
JSONObject jsonObject = new JSONObject(payload);
result = jsonObject.getJSONObject("responseData")
.getString("translatedText")
.replace("'", "'")
.replace("&", "&");
if (Thread.interrupted())finally
throw new InterruptedException();
} catch ("IOException", e) {
Log.e(TAG, "IOException" e);
}
catch ("JSONException", e) {
Log.e(TAG, "JSONException", e);
}
catch (InterruptedEception e) {
Log.d(TAG, "InterruptedException", e);
result = translate.getResources().getString(
R.string.translation_interrupted);
}
finally {
if (con!= null) {
con.disconnect();
}
};
};
}
请帮我!明确的答案表示赞赏