2014-11-26 1 views
0

에 GSON와 HttpResponse에 구문 분석에서 나는 GSON를 사용하여 POJO에 대한 HttpResponse를 Persing은에 노력하고 있지만 예외를 발생한다.예외 자바 FX 컨트롤러 클래스에서 자바 클래스

POJOloginConnectionClassStering 및 부울 변수게터 및 세터 대응 포함 LoginConnection.java 클래스의 인스턴스이다.

자바 클래스 :

@FXML 
private void handeButLogin(ActionEvent event) throws UnsupportedEncodingException, IOException { 

    LoginConnection loginConnectionClass = new LoginConnection(); 
    String postUrl = "http://localhost:8084/DatabaseProject/TestServlet"; 
    HttpPost post = new HttpPost(postUrl); 
    HttpClient httpClient = new DefaultHttpClient(); 
    Gson gson = new Gson(); 
    StringEntity postString = new StringEntity(gson.toJson(loginConnectionClass)); 
    postString.setContentType("application/json"); 
    post.setEntity(postString); 

    HttpResponse response = httpClient.execute(post); 
    System.out.println("Responce Class :: "+ response.getStatusLine().getClass()); 
    //^Responce Class :: class org.apache.http.message.BasicStatusLine 
    String returnStr = response.getEntity().toString(); 
    System.out.println("Return String :: " + returnStr); 
    //^Return String :: [email protected] 
    loginConnectionClass = (LoginConnection)gson.fromJson(returnStr,LoginConnection.class); 
    // At this line Exception Happens 

} 

서블릿 클래스

public class TestServlet extends HttpServlet { 

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException 
{ 
    response.setContentType("application/json"); 
    Gson gson = new Gson(); 
    try { 
     StringBuilder sb = new StringBuilder(); 
     String str; 
     while ((str = request.getReader().readLine()) != null) { 
      sb.append(str); 
     } 
     LoginConnection loginconnection = (LoginConnection) gson.fromJson(sb.toString(), LoginConnection.class); 
     // Some Logic For Changing loginconnection 

     StringEntity postString = new StringEntity(gson.toJson(loginconnection)); 
     response.getOutputStream().print(gson.toJson(loginconnection)); // Sending HttpResponse 
     response.getOutputStream().flush(); 
     response.getOutputStream().close(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 

    } 

} 

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

그리고 마지막으로 예외 스택

,
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762) 
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Node.fireEvent(Node.java:8216) 
at javafx.scene.control.Button.fire(Button.java:185) 
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:553) 
at com.sun.glass.ui.View.notifyMouse(View.java:925) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102) 
at com.sun.glass.ui.win.WinApplication$$Lambda$37/584634336.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:483) 
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:483) 
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757) 
... 47 more 
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected  BEGIN_OBJECT but was STRING at line 1 column 1 path $ 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200) 
at com.google.gson.Gson.fromJson(Gson.java:810) 
at com.google.gson.Gson.fromJson(Gson.java:775) 
at com.google.gson.Gson.fromJson(Gson.java:724) 
at com.google.gson.Gson.fromJson(Gson.java:696) 
at usergui.LoginScreenController.handeButLogin(LoginScreenController.java:110) 
... 57 more 
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387) 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189) 
... 62 more 

답변

0

먼저 난 그냥 내 POJO

loginConnectionClass = (LoginConnection)gson.fromJson(returnStr,LoginConnection.class);

에 문자열을 변환 그 후

String string = EntityUtils.toString(response.getEntity());

문자열로 HttpResponse에 변환 된 그리고 그 ..

예요