我想通过gson和一个类获得信息形式的mysql数据库。我总是得到同样的错误
错误
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Failed to invoke public plugin() with no args
at com.google.gson.internal.ConstructorConstructor$2.construct(ConstructorConstructor.java:98)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:163)
at com.google.gson.Gson.fromJson(Gson.java:755)
at com.google.gson.Gson.fromJson(Gson.java:694)
at gui_gimp$5.mouseClicked(gui_gimp.java:361)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6538)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
引起:java.lang.Error:未解决的编译问题: 令牌上的语法错误" void",volatile expected
at plugin.<init>(plugin.java:66)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.google.gson.internal.ConstructorConstructor$2.construct(ConstructorConstructor.java:91)
... 36 more
我的班级
public class plugin{
public int id = 0;
public String title = null;
public String link= null;
public String text = null;
public String cat= null;
public String end= null;
public String date= null;
public int rating = 0;
public int getId(){
return id;
}
public String gettitle(){
return title;
}
public void settitle(String title){
this.title = title;
}
public String getlink(){
return link;
}
public void setlink(String link){
this.link = link;
}
public String gettext(){
return text;
}
public void settext(String text){
this.text = text;
}
public String getcat(){
return cat;
}
public void setcat(String cat){
this.cat = cat;
}
public String getend(){
return end;
}
public void setend(String end){
this.end = end;
}
public String getdate(){
return date;
}
public void setdate(String date){
this.date = date;
}
public int getRating(){
return rating;
}
}
我的代码
String result ="";
try {
URL url = new URL("http://calc-o-meter.de/connect/getjson.php");
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ( (line = br.readLine()) != null)
result = result + line;
br.close();
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
try{
Gson gson = new Gson();
plugins = gson.fromJson(result, plugin[].class);
System.out.println("GSON "+result)
;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
答案 0 :(得分:-2)
首先使用Capitals作为班级名称
public class Plugin
然后正确地实现一切
Plugin[] plugins = gson.fromJson(result, Plugin[].class);
它会起作用