2012-09-27 43 views
0

我在向服務器發送POST請求時以及在處理服務器響應結果後有問題,但是當服務器寫入數據時有時數據無法傳送到客戶端,死於writer.write ()函數。 我使用Apache Tomcat部署應用程序,每次服務器需要響應gzip前的7m數據和gzip後的300k來自Android的HTTP POST在響應大小大時不返回數據大

請幫助!

我的代碼:

protected void dispatch(JSONObject request, 
       Map<String, FormDataItem> formItems, 
       HttpServletRequest servletRequest, 
       HttpServletResponse servletResponse, SecurityToken token, 
       String callback) throws JSONException, IOException { 
     long timeGetData = 0; 
     long begin = System.currentTimeMillis(); 

     String key = null; 

     if (request.has("id")) { 
       key = request.getString("id"); 
     } 

     // getRpcHandler never returns null 
     Future<?> future = getHandler(request, servletRequest).execute(
        formItems, token, jsonConverter); 

     timeGetData = System.currentTimeMillis(); 
     // Resolve each Future into a response. 
     ResponseItem response = getResponseItem(future); 
     ResponseItem responseTemp = (ResponseItem) response.getResponse(); 

     // Generate the output 
     Writer writer = servletResponse.getWriter(); 

     if ("syndata.getAllDataFromServer".equals(request.getString("method"))) { 
       if (responseTemp.getErrorCode() == 200) { 
        SynDataObject outObject = (SynDataObject) (responseTemp 
           .getResponse()); 
        writer.write(jsonConverter.convertToString(outObject 
           .getParams())); 
        writer.write("\r\n"); 

        for (int i = 0; i < outObject.getListTableData().size(); i++) { 
          TableDataObject subData = outObject.getListTableData().get(
             i); 
          int subDataLength = subData.getData().size(); 
          int currentFarm = 0; 
          int factor = 1000; 
          int lastIndex = 0; 

          while (lastIndex < subDataLength) { 
           if ((currentFarm * factor + factor) > subDataLength) { 
             lastIndex = (factor * currentFarm) 
                + (subDataLength - (factor * currentFarm)); 
           } else { 
             lastIndex = (factor * currentFarm) + factor; 
           } 

           TableDataObject chilData = new TableDataObject(); 
           chilData.setTableName(subData.getTableName()); 
           chilData.setTableColumns(subData.getTableColumns()); 
           chilData.setPkField(subData.getPkField()); 

           chilData.setData(subData.getData().subList(
              currentFarm * factor, lastIndex)); 


           writer.write(jsonConverter.convertToString(chilData)); 
           writer.write("\r\n"); 
           writer.flush(); 

           ++currentFarm; 
          } 
        } 
       } else { 
        Object result = getJSONResponse(key, response); 
        String content = jsonConverter.convertToString(result); 

        if (callback != null) { 
          writer.write(callback); 
          writer.write('('); 
        } 

        writer.write(content); 

        if (callback != null) { 
          writer.write(");\n"); 
        } 
       } 
     } else { 
       Object result = getJSONResponse(key, response); 
       String content = jsonConverter.convertToString(result); 

       if (callback != null) { 
        writer.write(callback); 
        writer.write('('); 
       } 

       writer.write(content); 

       if (callback != null) { 
        writer.write(");\n"); 
       } 
     } 

    } 
+0

你可以附加你得到的錯誤(堆棧跟蹤等)。爲此,您可以編輯原始帖子並添加信息。 –

+0

@HeikoRupp它只是僵局,等待很長時間,但沒有得到任何錯誤:( – QViet

回答

0

每個瀏覽器都有一些請求超時值。 如果服務請求所花費的時間或達到writer.write()行所用的時間超過了,則客戶端連接將關閉。

所以你不能寫任何信息給作家。

+0

我從Android客戶端Munesh請求:) – QViet