Google Translate V2 Java API throws JsonParseException when uses it behind proxy server

321 views
Skip to first unread message

Jason Xu

unread,
Dec 27, 2012, 7:22:59 AM12/27/12
to google-tra...@googlegroups.com
Hi Joao & Any Google Translate V2 Experts,
 
Pls help how to resolve following problem.
 
My project# is: 27384690894
I used Google Java API downloaded from http://code.google.com/p/google-translate-api-v2-java/
1) google-translate-api-v2-java-core-0.52.jar
2) jackson-core-asl-1.9.0.jar
3) httpcore-4.1.2.jar
4) httpclient-4.1.2.jar
5) jackson-mapper-asl-1.9.0.jar
6) jackson-jaxrs-1.9.0.jar
7) jackson-xc-1.9.0.jar
8) slf4j-api-1.6.2.jar
9) commons-codec-1.4.jar
10) commons-lang3-3.0.1.jar
 
When I run following code from office behind company proxy server, it throws exception: JsonParseException: Unexpected character ('<' (code 60)). 
If I run it from home without proxy server(by removing proxy setting and httpClient), it works perfect.
 
public Class GTranslator {
     
     public static void main(String[] args) throws IOException, URISyntaxException, Exception {
     
     DefaultHttpClient httpclient = new DefaultHttpClient();
     HttpHost proxy = new HttpHost("myproxyserver.xxx.com",8080);  
     httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
 
     Translator  translator = new Translator("myAPIKey", httpclient);
     testTranslate("Hello","zh-CN");  // translate from English to simple chinese
 
     public static void testTranslate(String source, String targetLang) throws IOException, URISyntaxException, TranslatorException {
        Translation fromEnglish = translator.translate(source, "en", targetLang);  
        System.out.println(source + " in en = '" + fromEnglish.getTranslatedText() + "' in " + targetLang);
     }
}
 
Exception (line# are not same as above codes):
Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: java.io.StringReader@9f5011; line: 1, column: 2]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
 at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:628)
 at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:362)
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2761)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2709)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
 at org.google.translate.api.v2.core.Translator.readResponse(Translator.java:274)
 at org.google.translate.api.v2.core.Translator.translate(Translator.java:124)
 at org.google.translate.api.v2.core.Translator.translate(Translator.java:87)
 at ncs.mom.gt.GTranslator.testTranslate(GTranslator.java:111)
 at ncs.mom.gt.GTranslator.main(GTranslator.java:79)
 
Note:
When I run it from home without  proxy server by changing following 4 lines to one and remove httpClient, translation works perfect.
Change:
     DefaultHttpClient httpclient = new DefaultHttpClient();
     HttpHost proxy = new HttpHost("myproxyserver.xxx.com",proxy_port#);  
     httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
     Translator  translator = new Translator("myAPIKey", httpclient);
To:
     Translator  translator = new Translator("myAPIKey");
 
 
Thanks in advance.
Jason
 

Jason Xu

unread,
Dec 30, 2012, 9:03:24 PM12/30/12
to google-tra...@googlegroups.com
Hi
 
Any Google Technical Support can help on this?
 
Did I miss out any thing in the setting ?
Do I have to set the user/pwd in httpClient, if yes, how to do this?
Any working code example to perfrom the translate API V2 Java call behind a proxy server ?
 
Thanks & Regards,
Jason

João Martins

unread,
Jan 2, 2013, 4:44:47 AM1/2/13
to google-tra...@googlegroups.com
I see you are using an unofficial wrapper for the Translate API. Were you able to see what the response message is like? Seems like a response is sent by the proxy server, just not in valid JSON.
Also, does your API key allow access to any referrer?

Jason Xu

unread,
Jan 16, 2013, 2:02:44 AM1/16/13
to google-tra...@googlegroups.com
Hi Joao,
 
Thanks for your reply.
 
I finally get it working.
 
Just to share with all.
1) I get the latest Google Translate offical wrapper.
2) Download and replace jar file jackson-mapper-asl-1.9.0.jar with version jackson-mapper-asl-1.9.2.jar.
3) It is working now.
 
 
Best Regards,
Jason
Reply all
Reply to author
Forward
0 new messages