2

我真的需要一个如何使用 google translate API v2 翻译文本的示例。

我已经实现了以下内容:

String googleUrl="https://www.googleapis.com/language/translate/v2?key=<My Key>";
googleUrl+="&q=";
googleUrl+=urlEncode(txtFeedback.getString());
googleUrl+="&source=";
googleUrl+=System.getProperty("microedition.locale").substring(0, 2);
googleUrl+="&target=en";
HttpConnection googlAPI = null;
DataInputStream dis = null;

StringBuffer response = new StringBuffer();
googlAPI = (HttpConnection)Connector.open(googleUrl);


googlAPI.setRequestMethod(HttpConnection.GET);
dis = new DataInputStream(googlAPI.openInputStream());
int ch;
while ((ch = dis.read()) != -1) {
    response.append((char) ch);
}


String tt = response.toString();
tt = tt.substring(tt.indexOf("{"));
JSONObject js = new JSONObject(tt);
params +=js.getJSONObject("data").getJSONArray("translations").getJSONObject(0)
              .getString("translatedText") + crlf;

但此代码引发证书异常:证书是由无法识别的实体颁发的

它在我的真实设备三星 GT-S5230以及模拟器上引发异常

真的需要帮助。

如果我做错了什么,最好能获得一个如何从 j2me midlet 调用谷歌翻译 API 的示例。

4

1 回答 1

1

快速浏览显示您正在访问https url:

字符串 googleUrl="https://www.googleapis.com/language/translate/v2?key=";

使用 httpConnection

googlAPI = (HttpConnection) Connector.open(googleUrl);

将其更改为 HttpsConnection

HttpsConnection googlAPI = null;
...
googlAPI = (HttpsConnection) Connector.open(googleUrl);

让我们看看情况如何。

于 2012-03-23T04:10:07.963 回答