我想写一个翻译程序,,,用了有道api然后我在控制台运行没有问题但是由GUI来操作的话就会报java.net.SocketException: Address family not supported by protocol family: connect 异常。。以下是翻译的类package translate;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;public class Translate extends Thread {
String tra;
TranslateWord tw;
TranslateView tv;
public Translate(String tString,TranslateView tv) {
this.tra=tString;
this.tv=tv;
} public void run() {
if (tra!=null) {
this.tw=this.getWord(this.translate(this.tra));
tv.setTw(tw);
tv.set();
}
}
public Translate() {
}
@SuppressWarnings({ "null", "unused" })
public TranslateWord getWord(String json) {
JsonElement je = null;
TranslateWord tw = null;
try {
if (json==null) {
return null;
}
tw=new TranslateWord();
JsonParser jsp = new JsonParser();
// JsonObject object = (JsonObject) jsp.parse(new FileReader(new File(
// "Untitled.json")));
JsonObject object=(JsonObject) jsp.parse(json);
if ((je = object.get("errorCode")) != null) {
tw.setErrorCode(je.getAsInt());
if (tw.getErrorCode() != 0) {
return null;
}
} if ((je = object.get("translation")) != null) {
JsonArray ja = je.getAsJsonArray();
String t[] = new String[ja.size()];
for (int i = 0; i < t.length; i++) {
t[i] = new String(ja.get(i).getAsString());
}
tw.setTranslation(t);
}
if ((je = object.get("query")) != null) {
tw.setQuery(je.getAsString());
} if ((je = object.get("web")) != null) {
JsonArray ja = je.getAsJsonArray();
Web web[] = new Web[ja.size()];
for (int i = 0; i < web.length; i++) {
web[i] = new Web();
}
tw.setWeb(web);
for (int i = 0; i < tw.getWeb().length; i++) {
JsonObject jse = (JsonObject) ja.get(i);
if ((je = jse.get("value")) != null) {
String t[] = new String[je.getAsJsonArray().size()];
for (int j = 0; j < t.length; j++) {
t[j] = new String(je.getAsJsonArray().get(j)
.getAsString());
} (tw.getWeb())[i].setValue(t);
}
if ((je = jse.get("key")) != null) {
tw.getWeb()[i].setKey(je.getAsString());
}
} }
if ((je = object.get("basic")) != null) {
Basic basic = new Basic();
JsonObject jso = je.getAsJsonObject();
if ((je = jso.get("us-phonetic")) != null) {
basic.setUs_phonetic(je.getAsString());
}
if ((je = jso.get("uk-phonetic")) != null) {
basic.setUk_phonetic(je.getAsString());
}
if ((je = jso.get("phonetic")) != null) {
basic.setPhonetic(je.getAsString());
}
if ((je = jso.get("explains")) != null) {
JsonArray js = je.getAsJsonArray();
String[] t = new String[js.size()];
for (int i = 0; i < t.length; i++) {
t[i] = new String(js.get(i).getAsString());
}
basic.setExplains(t);
}
tw.setBasic(basic);
}
// System.out.println(tw.toString()); // System.out.println(object.get("query").getAsString()); } catch (JsonIOException | JsonSyntaxException e) {
e.printStackTrace();
}
return tw;
} public static void main(String[] args) throws FileNotFoundException {
// translate("welcome");
Translate t=new Translate();
System.out.println(t.getWord(t.translate("很高兴认识你")));
} public String translate(String translate) {
StringBuffer sb = new StringBuffer();
try {
translate = URLEncoder.encode(translate, "utf-8");
URL url = new URL(
"http://fanyi.youdao.com/openapi.do?keyfrom=inkbox&key=1184033755&type=data&doctype=json&version=1.1&q="
+ translate); URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
isr.close();
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;public class Translate extends Thread {
String tra;
TranslateWord tw;
TranslateView tv;
public Translate(String tString,TranslateView tv) {
this.tra=tString;
this.tv=tv;
} public void run() {
if (tra!=null) {
this.tw=this.getWord(this.translate(this.tra));
tv.setTw(tw);
tv.set();
}
}
public Translate() {
}
@SuppressWarnings({ "null", "unused" })
public TranslateWord getWord(String json) {
JsonElement je = null;
TranslateWord tw = null;
try {
if (json==null) {
return null;
}
tw=new TranslateWord();
JsonParser jsp = new JsonParser();
// JsonObject object = (JsonObject) jsp.parse(new FileReader(new File(
// "Untitled.json")));
JsonObject object=(JsonObject) jsp.parse(json);
if ((je = object.get("errorCode")) != null) {
tw.setErrorCode(je.getAsInt());
if (tw.getErrorCode() != 0) {
return null;
}
} if ((je = object.get("translation")) != null) {
JsonArray ja = je.getAsJsonArray();
String t[] = new String[ja.size()];
for (int i = 0; i < t.length; i++) {
t[i] = new String(ja.get(i).getAsString());
}
tw.setTranslation(t);
}
if ((je = object.get("query")) != null) {
tw.setQuery(je.getAsString());
} if ((je = object.get("web")) != null) {
JsonArray ja = je.getAsJsonArray();
Web web[] = new Web[ja.size()];
for (int i = 0; i < web.length; i++) {
web[i] = new Web();
}
tw.setWeb(web);
for (int i = 0; i < tw.getWeb().length; i++) {
JsonObject jse = (JsonObject) ja.get(i);
if ((je = jse.get("value")) != null) {
String t[] = new String[je.getAsJsonArray().size()];
for (int j = 0; j < t.length; j++) {
t[j] = new String(je.getAsJsonArray().get(j)
.getAsString());
} (tw.getWeb())[i].setValue(t);
}
if ((je = jse.get("key")) != null) {
tw.getWeb()[i].setKey(je.getAsString());
}
} }
if ((je = object.get("basic")) != null) {
Basic basic = new Basic();
JsonObject jso = je.getAsJsonObject();
if ((je = jso.get("us-phonetic")) != null) {
basic.setUs_phonetic(je.getAsString());
}
if ((je = jso.get("uk-phonetic")) != null) {
basic.setUk_phonetic(je.getAsString());
}
if ((je = jso.get("phonetic")) != null) {
basic.setPhonetic(je.getAsString());
}
if ((je = jso.get("explains")) != null) {
JsonArray js = je.getAsJsonArray();
String[] t = new String[js.size()];
for (int i = 0; i < t.length; i++) {
t[i] = new String(js.get(i).getAsString());
}
basic.setExplains(t);
}
tw.setBasic(basic);
}
// System.out.println(tw.toString()); // System.out.println(object.get("query").getAsString()); } catch (JsonIOException | JsonSyntaxException e) {
e.printStackTrace();
}
return tw;
} public static void main(String[] args) throws FileNotFoundException {
// translate("welcome");
Translate t=new Translate();
System.out.println(t.getWord(t.translate("很高兴认识你")));
} public String translate(String translate) {
StringBuffer sb = new StringBuffer();
try {
translate = URLEncoder.encode(translate, "utf-8");
URL url = new URL(
"http://fanyi.youdao.com/openapi.do?keyfrom=inkbox&key=1184033755&type=data&doctype=json&version=1.1&q="
+ translate); URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
isr.close();
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}}
import javax.swing.JFrame;
import javax.swing.JPanel;import java.awt.BorderLayout;
import java.awt.Dimension;import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.UIManager;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.UnsupportedLookAndFeelException;import java.awt.Font;import javax.swing.LayoutStyle.ComponentPlacement;import com.birosoft.liquid.LiquidLookAndFeel;import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.geom.QuadCurve2D;
import java.io.FileNotFoundException;public class TranslateView extends JFrame { private JPanel contentpanel;
private JTextField translationText;
private JLabel phonetic;
private JLabel translation;
private JLabel explains;
private JLabel textField_4;
private JLabel values;
private JLabel key;
private TranslateWord tw;
private JLabel query;
public void setTw(TranslateWord tw) {
this.tw=tw;
}
public TranslateView() {
try {
UIManager.setLookAndFeel(new LiquidLookAndFeel());
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
this.setTitle("墨盒的翻译工具----有道");
this.setSize(345, 357);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null); contentpanel = new JPanel();
this.setContentPane(contentpanel);
contentpanel.setLayout(new BorderLayout(0, 0)); JPanel panel = new JPanel();
contentpanel.add(panel, BorderLayout.NORTH); translationText = new JTextField();
panel.add(translationText);
translationText.setColumns(20); JButton button = new JButton("翻译");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (translationText.getText() != null
&& translationText.getText().length() > 0) {
// Translate.translate(translationText.getText());
//tw= Translate.getWord(Translate.translate(translationText.getText()));
//set();
Translate t=new Translate();
// t.post(translationText.getText());
t.start();
// tw=t.getWord(t.translate(translationText.getText()));
}
}
});
panel.add(button); JPanel panel_1 = new JPanel();
contentpanel.add(panel_1, BorderLayout.CENTER); query = new JLabel("欢迎使用inkbox翻译");
query.setFont(new Font("黑体", Font.BOLD, 17)); phonetic = new JLabel();
phonetic.setText("音标:");
// phonetic.setColumns(10); translation = new JLabel();
translation.setText("翻译:");
// translation.setColumns(10); explains = new JLabel();
explains.setText("释义");
// explains.setColumns(10); textField_4 = new JLabel();
textField_4.setText("以下内容来自网络");
// textField_4.setColumns(10); values = new JLabel();
values.setText("values:");
// values.setColumns(10); key = new JLabel();
key.setText("key:");
// key.setColumns(10);
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(
Alignment.LEADING).addGroup(
gl_panel_1
.createSequentialGroup()
.addContainerGap()
.addGroup(
gl_panel_1
.createParallelGroup(Alignment.LEADING)
.addComponent(explains,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE)
.addComponent(query,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE)
.addComponent(translation,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE)
.addComponent(phonetic,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE)
.addComponent(textField_4,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE)
.addComponent(values,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE)
.addComponent(key,
GroupLayout.DEFAULT_SIZE, 309,
Short.MAX_VALUE))
.addContainerGap()));
gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(
Alignment.LEADING).addGroup(
gl_panel_1
.createSequentialGroup()
.addComponent(query, GroupLayout.PREFERRED_SIZE, 37,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(translation, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(phonetic, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(explains, GroupLayout.PREFERRED_SIZE, 66,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(values, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(key, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE)));
panel_1.setLayout(gl_panel_1);
this.setMinimumSize(new Dimension(345, 357));
this.setVisible(true); }
public void set(){
if (tw!=null) {
if (tw.getErrorCode()!=0) {
JOptionPane.showMessageDialog(this, "无法翻译!");
return;
}
query.setText(tw.getQuery());
StringBuffer sb=new StringBuffer("翻译:");
for (int i = 0; i < tw.getTranslation().length; i++) {
sb.append(tw.getTranslation()[i]+" ");
}
translation.setText(sb.toString());
phonetic.setText("音标"+tw.getBasic().getPhonetic());
sb=new StringBuffer("释义:");
for (int i = 0; i < tw.getBasic().getExplains().length; i++) {
sb.append(tw.getBasic().getExplains()[i]+" ");
}
explains.setText(sb.toString());
sb=new StringBuffer("values:");
sb.append(tw.getWeb()[0].getKey()+" ");
for (int i = 0; i < tw.getWeb()[0].getValue().length; i++) {
sb.append(tw.getWeb()[0].getValue()[i]+" ");
}
values.setText(sb.toString());
}
}
public static void main(String[] args) {
new TranslateView();
}
}
String translation[]; public String[] getTranslation() {
return translation;
} public void setTranslation(String[] translation) {
this.translation = translation;
} public String getQuery() {
return query;
} public void setQuery(String query) {
this.query = query;
} public int getErrorCode() {
return errorCode;
} public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
} public Basic getBasic() {
return basic;
} public void setBasic(Basic basic) {
this.basic = basic;
} public Web[] getWeb() {
return web;
} public void setWeb(Web[] web) {
this.web = web;
} private String query;
private int errorCode;// 错误代码 private Basic basic;
private Web web[]; /**
* 0 - 正常 20 - 要翻译的文本过长 30 - 无法进行有效的翻译 40 - 不支持的语言类型 50 - 无效的key 60 -
* 无词典结果,仅在获取词典结果生效
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(" Translation=");
if (this.getTranslation() != null) {
for (int i = 0; i < this.getTranslation().length; i++) {
sb.append(this.getTranslation()[i] + " ");
}
}
sb.append(" query=" + this.getQuery());
sb.append(" errorCode=" + this.getErrorCode());
sb.append(" web=");
if (this.getWeb() != null) {
for (int i = 0; i < this.getWeb().length; i++) {
// System.out.println("webs="+web[i].toString());
sb.append(this.getWeb()[i] + " ");
}
}
if (this.getBasic()!=null) {
sb.append(" basic=" + this.getBasic().toString());
}
// System.out.println("basic="+this.getBasic().toString());
return sb.toString();
}}class Basic {
private String us_phonetic; public String getUs_phonetic() {
return us_phonetic;
} public void setUs_phonetic(String us_phonetic) {
this.us_phonetic = us_phonetic;
} public String getPhonetic() {
return phonetic;
} public void setPhonetic(String phonetic) {
this.phonetic = phonetic;
} public String getUk_phonetic() {
return uk_phonetic;
} public void setUk_phonetic(String uk_phonetic) {
this.uk_phonetic = uk_phonetic;
} public String[] getExplains() {
return explains;
} public void setExplains(String[] explains) {
this.explains = explains;
} private String phonetic;
private String uk_phonetic;
private String explains[]; public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(" us-phonetic=" + this.getUs_phonetic());
sb.append(" uk-phonetic=" + this.getUk_phonetic());
sb.append(" phonetic=" + this.getPhonetic());
if (this.getExplains() != null) {
sb.append(" explains=");
for (int i = 0; i < this.getExplains().length; i++) {
sb.append(this.getExplains()[i] + " ");
}
}
return sb.toString();
}}class Web {
private String value[]; public String[] getValue() {
return value;
} public void setValue(String[] value) {
this.value = value;
} public String getKey() {
return key;
} public void setKey(String key) {
this.key = key;
} private String key; @Override
public String toString() {
StringBuffer sb = new StringBuffer();
if (this.getValue() != null) {
sb.append(" values=");
for (int i = 0; i < this.getValue().length; i++) {
if (i != this.getValue().length - 1) {
sb.append(this.getValue()[i] + ",");
} else {
sb.append(this.getValue()[i]);
}
}
}
sb.append(" Key=" + this.getKey() + " ");
return sb.toString();
}}