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-stringが正しく作成されています。次のようになります。
{"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)
解決策はありますか?