-3

I have written a web-service in Java. This web-service is hosted in TOMCAT. I am returning a JSON string. The JSON string is as follows:

accountDetailsNodes = [{mobileNumber=01948330292, errorMessage=null, customerCode=59744000002, photo=a string of 35536 charaters , accountOpenDate=null, errorFlag=N, customerNumber=4, customerName=Md. Saifur Hossain , accountID=2, accountTypeId=13, accountTypeDescription=Savings Account, customerPointId=1, balance=100000037640.50, accountTile=Md. Saifur Hossain}]

The length of the JSON string is 32613. But the full response is not coming to android apps. I think there may be some limitation on sending response from Tomcat. How can I overcome this limitation of Tomcat?

Updated:

This is my code to generate JSON.

try {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            JSONObject json = new JSONObject();
            CashDepositDao dao = new CashDepositDao();
            for (CashDepositModel bo : dao.getAccountDetals(accountNo,branchCode)) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("accountTile", bo.getAccountTitle());
                map.put("accountOpenDate", bo.getAccountOpenDate());
                map.put("mobileNumber", bo.getMobileNumber());
                map.put("balance", bo.getBalance());
                map.put("accountTypeId", bo.getAccountTypeID());
                map.put("accountTypeDescription", bo.getAccountTypeDescription());
                map.put("accountID", bo.getAccountID());
                map.put("customerNumber", bo.getCustomerNumber());
                map.put("customerCode", bo.getCustomerCode());
                map.put("customerName", bo.getCustomerName());
                map.put("customerPointId", bo.getCustomerPointID());
                map.put("photo", bo.getPhoto());
                map.put("errorMessage", bo.getErrorMessage());
                map.put("errorFlag", bo.getErrorFlage());

                list.add(map);
                json.put("accountDetailsNodes", list);

            }
            System.out.println("accountDetailsNodes = " + list);
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            // System.out.println("Response Completed... ");
        } catch (Exception ex) {
            Logger.getLogger(SourecAccountDetailsSV.class.getName()).log(Level.SEVERE, null, ex);
        }

Sending And Getting response from Mobile App:

I am sending and getting the response using the following code:

public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params)); 
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url); 
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }          

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            System.out.println(json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    } 

I have printed the string received in this method . Surprisingly, the full string is not received in this method.

user207421
  • 305,947
  • 44
  • 307
  • 483
Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68

2 Answers2

0

How can I overcome this limitation of tomcat ?

Tomcat can send arbitrary length strings, and even if there was a limit, it wouldn't be measured in kilobytes but in orders of magnitude more. There is no tomcat limitation that you need to overcome. If your browser receives the full string, so can any other app.

As you're using json.toString() anyways, you could explicitly set the Content-Length header and see if this makes a difference. Stop worrying about Tomcat and double check if your Android App has some problems parsing a json response of this size, or if any network component in between limits your response in some way.

Edit: It's not Tomcat's problem, it's on the Android side and your answer is in the comments to the question.

The first problem is in what's proposed as "duplicate" question: You must not compare String with == as you do. Add else System.out.print("unexpected"); to your first if/else block to illustrate.

The second problem is that we have no clue where you get is from. As it looks now, it could be overridden by parallel requests (it's probably a class member?) - or due to the wrong string comparison never be initialized at all (leading to your problem that you can't see any content at all on the Android side, despite tomcat sending it). Make it a local variable, as proposed by EJP in his/her comment.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • The servlet container sets `Content-length` automatically. Setting it yourself won't make any difference. It will get overridden by the container. – user207421 Sep 06 '17 at 12:12
0

I think there may be some limitation on sending response from Tomcat.

There isn't.

How can I overcome this limitation of Tomcat?

There is no such limitation.

I am sending and getting the response using the following code:

public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){

Here you are incorrectly comparing strings. You should use equals(), not ==.

                // ...
            }else if(method == "GET"){

Ditto.

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);

Here you are using is which may not have been initialized at all. It appears to be a member variable, so it is probably still null, so at this point I would expect a NullPointerException. is should of course be a local variable in this method.

I have printed the string received in this method. Surprisingly, the full string is not received in this method.

What is surprising is that anything is received, if that's what you're claiming. I would have expected an NPE.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The string is partially received . Total character of json string from web-service is 32613 . But in android app , only 4567 characters are received . – Christopher Marlowe Sep 07 '17 at 04:38