私は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();
}
};
};
}
私を助けてください!明確な答えをいただければ幸いです