java.lang.RuntimeException:失败:HTTP错误代码:500

时间:2015-07-13 12:51:31

标签: java json

我正在尝试检索json对象,然后使用其中的信息。但在Json请求时,它正在抛出一个运行时异常。 这是代码

public URL getURL(){                

            try{                    
                URL domain = new URL("http.....");                  
                URL url = new URL(domain + cookieValue);                    
                System.out.println(url);                    
                return url;
            }
            catch (MalformedURLException ex) {                
                log_loginBean.logger(ex);
            }

            return null;    
        }

    public static JSONObject JSONRequest(URL url_comp){

                try {                        
                    URL url = url_comp;
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Accept", "application/json");

                    if (conn.getResponseCode() != 200) {
                        throw new RuntimeException("Failed : HTTP error code : "
                                + conn.getResponseCode());
                    }

                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    String jsonText = readAll(br);
                    JSONObject json = new JSONObject(jsonText);       
                    conn.disconnect();                      
                    return json;

                  } 
                  catch (MalformedURLException e) {              
                      log_loginBean.logger(e);               
                  } 
                  catch (IOException e) {                
                      log_loginBean.logger(e);

                  } 
                  catch(JSONException e) {                      
                      log_loginBean.logger(e);
                  }

                return null;        
            }

        // This will extract some particular information (UserId, Email) from the extracted JSON Object. 

            public void parseJSON(){
              try{
                JSONObject obj = JSONRequest(getURL());
                userId = obj.getString("UserID");
                Email = obj.getString("Email");                 

              }
              catch(JSONException e){
                  log_loginBean.logger(e);
              } 
            }

这是我的堆栈跟踪

    SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/CDCandTR] threw exception [Failed : HTTP error code : 500] with root cause
java.lang.RuntimeException: Failed : HTTP error code : 500
            at com.walmart.cdctraffic.login.LoginBean.JSONRequest(LoginBean.java:183)
            at com.walmart.cdctraffic.login.LoginBean.parseJSON(LoginBean.java:226)
            at com.walmart.cdctraffic.login.LoginBean.login(LoginBean.java:266)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
            at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
            at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
            at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:128)
            at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2603)
            at javax.faces.event.SystemEvent.processListener(SystemEvent.java:108)
            at javax.faces.event.ComponentSystemEvent.processListener(ComponentSystemEvent.java:118)
            at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2187)
            at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:2135)
            at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:289)
            at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:247)
            at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:107)
            at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
            at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
            at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at com.walmart.cdctraffic.utilities.ResponseHeaderFilter.doFilter(ResponseHeaderFilter.java:30)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
            at com.walmart.infraweb.valves.SecureErrorReportValve.invoke(SecureErrorReportValve.java:102)
            at org.apache.catalina.valves.StuckThreadDetectionValve.invoke(StuckThreadDetectionValve.java:221)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
            at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
            at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
            at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2440)
            at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2429)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            at java.lang.Thread.run(Thread.java:745)

所以现在我无法找到它为什么会抛出HTTP 500错误以及如何解决所有这些问题? 然后在IF语句之后,我是否需要放入else并断开连接? 请帮忙。

2 个答案:

答案 0 :(得分:1)

根据提供的堆栈跟踪,我怀疑您没有正确传递登录信息,因为它在LoginBean中的某处明显失败:

com.walmart.cdctraffic.login.LoginBean.JSONRequest(LoginBean.java:183)

答案 1 :(得分:0)

几乎任何原因都可以返回500 Internal Server Error。最常见的是我不理解您的请求

下一个最常见的是我知道你做错了什么,但我不能把这些知识一直传回给回复

我最近遇到的这个问题最终证明是我发送的请求包含一个被服务器后面的数据库模式拒绝的值。我需要0/1时发送“文字”。