GAE 1.7.4中的IOException

时间:2013-04-04 02:38:24

标签: android google-app-engine ioexception

我有问题从我的gae-server向客户端发送数据。一切都在本地环境中工作,但在Google的服务器环境中没有,我得到IOException。

首先,我将json文件发送到我的gae服务器......

url = new URL("http://xxxserver.appspot.com/xxxserver")
               HttpURLConnection connection = (HttpURLConnection)url.openConnection();
           connection.setRequestMethod("POST");
           connection.setRequestProperty( "Content-Type", "application/json" );
           connection.setRequestProperty("Content-Length", "" + json.toString().getBytes("UTF8").length);
           connection.setRequestProperty("Content-Language", "en-US");  
           connection.setUseCaches (false);
           connection.setDoInput(true);
           connection.setDoOutput(true);    
           connection.setConnectTimeout(40000); //40 seconds

json-string,whick应该发送回来是正确创建的。它看起来如下:

{"location1 cost_char":"0.77","location1 cost_reim":"0.0","location2 dist":"34.02","location1 dist":"2.76"}

服务器接收文件,正确创建json-string,但是当它应该刷回json-string时会崩溃并抛出IOException:

//Send data to server
    try {   
    resp.setContentLength(json.toString().getBytes("UTF8").length);
    resp.setContentType("application/json");
        Writer wr = new BufferedWriter (new OutputStreamWriter (resp.getOutputStream()));           
         try {
             wr.write(json.toString());
             wr.flush();        //crash!!!!!!!!!!!!!
         } 
         finally {
             if (wr != null)
                    logger.info(TAG +"test6");
                 wr.close();
         }
    }
    catch (IOException e) {
        e.printStackTrace();
        logger.info(TAG +": IO Error");
        String temp = getStackTrace (e);
    logger.warning(TAG +": Exception is: " +temp);
    }
    catch (Exception e) {   
        e.printStackTrace();
        logger.warning(TAG +": Exception is: " +e);
    }

客户端接收数据:

//Get response from server
            try {
                InputStreamReader is = new InputStreamReader (connection.getInputStream());
                BufferedReader rd = new BufferedReader(is);  //BufferedInputStreams works on byte streams.  BufferedReaders works on character streams.
                String line;
                response = new StringBuffer();     
               try {
                   while((line = rd.readLine()) != null) {
                       response.append(line);
                       Log.d(TAG, "Received Data: " +line);
                   }
                   Log.v("Abrage:" ,"data received");
               }
               finally {
                   if (rd != null)
                       rd.close();
               }

这是StackTrace:

com.lemtex.carpoolio.server.CarpoolioServerServlet doPost: CarpoolioServerServlet: Exception is: java.io.IOException: Closed
at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:629)
at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:579)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:220)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:290)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:294)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:140)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.BufferedWriter.flush(BufferedWriter.java:253)
at com.lemtex.carpoolio.server.CarpoolioServerServlet.doPost(CarpoolioServerServlet.java:387)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:57)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:447)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:466)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:473)
at com.google.tracing.TraceContext.runInContext(TraceContext.java:694)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:338)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:330)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:470)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
at java.lang.Thread.run(Thread.java:679)

任何解决方案?

2 个答案:

答案 0 :(得分:0)

resp.setContentLength(json.toString().getBytes("UTF8").length);

可能与您使用.toString()发送的长度不同,特别是如果它不是纯ASCII,我猜您在ASCII集之外发送扩展字符。

wr.write(json.toString());

并且服务器可能在写入整个字符串之前关闭连接。 获取String的实际长度或发送您获得长度的.getBytes("UTF8")版本。你不能像你一样做到这一半。

您没有显示足够的代码来实际诊断问题,我们所能做的只是猜测。

答案 1 :(得分:0)

好吧,我解决了我的问题..我关闭了OutputStream 在发送数据之前resp.getOutputStream().close(); ....