2011-12-14 198 views
2

我真的需要一个例子如何翻译文本与谷歌翻译API V2。j2me谷歌翻译API

我已经实现已经执行以下操作:

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的例子。

回答

1

快速查看显示您正在访问HTTPS网址:

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

使用的HttpConnection

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

改变,要HttpsConnection

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

,让我们看看怎么回事。

+0

嗨Opeyemi,感谢您的建议,但它没有帮助。 – okarpov 2012-05-31 18:37:09