1

我在将数据从我的 gae 服务器发送到客户端时遇到问题。一切都在本地环境中运行,但在 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 字符串。它看起来如下:

{"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();
               }

这是堆栈跟踪:

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)

有什么解决办法吗?

4

2 回答 2

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

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

wr.write(json.toString());

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

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

于 2013-04-04T02:52:30.837 回答
0

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

于 2013-04-06T13:09:00.523 回答