Search

Wednesday, December 19, 2007

Proxy setting in java program

To day I programming java with google translate api but in my organization use a proxy server. I try several time to solve and search on google.com until I found answer below java code for use google translate api.


import java.net.*;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class TranslateTest {

public String stringText;
public static void main(String[] args) {

System.setProperty("http.proxyHost", "you_proxy_server");
System.setProperty("http.proxyPort","you_proxy_port");

String s = "Despite our best attempts at trashing the planet in hopes that Al Gore will grow a beard again," +
" we can't help but love repurposed electronics. The Plink Jet, spotted at NYU's ITP Winter Show 2007, " +
"uses the back-and-forth mechanisms out of four old ink jets to slide up and down four guitar strings, " +
"with various knob-controlled settings to control pitch and strumming patterns. The resulting noise is" +
" nice hybrid of robotic clicking and analog twang, but we hear HP charges a fortune for the strings.";
TranslateTest t = new TranslateTest();
if(t.translate(s)){
System.out.println(t.stringText);
}else
System.out.print("Translate false");
}
public boolean translate(String st){
try{
this.stringText = Translate.translate(st,Language.ENGLISH, Language.JAPANESE);
return true;
}catch (Exception ex){
ex.printStackTrace();
return false;
}
}

}