Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:18:10+00:00 2026-06-14T13:18:10+00:00

We are getting some very weird behavior in Android. Our network stack (that talks

  • 0

We are getting some very weird behavior in Android. Our network stack (that talks to a REST server) works fine in almost all situations, except when we do a GET shortly after doing a larger POST. What appears to be happening is that the Output stream is not flushing, and ends up sending the last line that was in there when the new socket is opened. Please note, each connection is a new object created, so this is unexpected behavior. First, the error code that seems to point me to the output stream, these are from the server logs.

10.1.8.195 – – [07/Nov/2012:13:36:28 -0700] “POST /iou/lender HTTP/1.1” 200 28 “-” “Android”
10.1.8.195 – – [07/Nov/2012:13:36:36 -0700] “————V2ymHFg03ehbqgZCaKO6jy” 400 173 “-” “-“

That attempt after should be a GET that then pulls the data from the server that includes the new entry added via the POST. However, all we get is again what appears to be the last line from the output stream from the POST. Here is our core code for the network stack, if more of the surrounding code is needed, let me know.

public Object serverConnect(String url, String method,
        Hashtable<String, Object> params) {

    HttpConnection c = null;
    InputStream is = null;
    OutputStream out = null;
    ByteArrayOutputStream postDataByteArrayImage = new ByteArrayOutputStream();
    byte[] data;
    String boundry = "----------V2ymHFg03ehbqgZCaKO6jy";
    try {
        if (!url.startsWith("/")) {
            url = "/" + url;
        }
        String uri = Control.URL_Secure + Control.dtserver + ":"
                + Control.port + url;
        ByteArrayOutputStream postDataByteArray = new ByteArrayOutputStream();

        params.put("sessionId", Control.sessionId);

        if (method.equals("GET")) {
            uri = uri + "?";
            Enumeration enumParams = params.keys();
            while (enumParams.hasMoreElements()) {
                if (!uri.endsWith("?")) {
                    uri = uri + "&";
                }
                String key = (String) enumParams.nextElement();
                uri = uri
                        + key
                        + "="
                        + java.net.URLEncoder.encode((String) params
                                .get(key));

            }
        } else if (method.equals("POST")) {
            Enumeration enumParams = params.keys();
            postDataByteArray.write(("--").getBytes());
            postDataByteArray.write((boundry).getBytes());
            postDataByteArray.write(("\r\n").getBytes());
            while (enumParams.hasMoreElements()) {
                String key = (String) enumParams.nextElement();
                if (!key.equals("image")){
                    postDataByteArray
                            .write(("Content-Disposition: form-data; name=\"")
                                    .getBytes());
                    postDataByteArray.write((key).getBytes());
                    postDataByteArray.write(("\"").getBytes());
                    postDataByteArray.write(("\r\n\r\n").getBytes());
                    postDataByteArray.write(((String) params.get(key))
                            .getBytes());
                    postDataByteArray.write(("\r\n").getBytes());
                    postDataByteArray.write(("--").getBytes());
                    postDataByteArray.write(boundry.getBytes());
                    postDataByteArray.write(("\r\n").getBytes());
                }
            }
            postDataByteArray.close();

        }
        Log.i("URL", uri);
        URL urltoConenct = new URL(uri);
        URLConnection connection = urltoConenct.openConnection();
        HttpURLConnection urlConnection = (HttpURLConnection) connection;

        URLConnection.setDefaultRequestProperty("Method", method); // default
        urlConnection.setRequestProperty("User-Agent", "Android");
        if (method.equals("POST")) {
            urlConnection.setDoOutput(true);
            urlConnection.setFixedLengthStreamingMode(postDataByteArray.toByteArray().length + postDataByteArrayImage.toByteArray().length);
            urlConnection.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=" + boundry);
            out = urlConnection.getOutputStream();
            out.write(postDataByteArray.toByteArray());
            out.write(postDataByteArrayImage.toByteArray());
            out.close();
        }
        int response = 0;
        try {
            response = urlConnection.getResponseCode();
        } catch (IOException e) {
            if (e.toString()
                    .equals("java.io.IOException: Received authentication challenge is null"))
                throw new RESTException(401, "Invalid Phone or Pin");
            else
                throw e;
        }

        if (response == HttpURLConnection.HTTP_OK) {

            is = urlConnection.getInputStream();
            if (is == null) {
                return new IOException(
                        "Cannot open HTTP InputStream, aborting");
            }

            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            int ch;
            int count = 0;

            while ((ch = is.read()) != -1) {
                bo.write(ch);
                count++;

            }
            data = bo.toByteArray();

            return new String(data);
        } else if (response == 500) {
            return new RESTException(500, "Internal server error");
        } else {
            RESTException x = new RESTException();
            x.setCode(response);
            try {
                is = urlConnection.getInputStream();
                if (is == null) {
                    x.setMessage("Unable to retrieve message");
                    return x;
                }

                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                int ch;
                int count = 0;

                while ((ch = is.read()) != -1) {
                    bo.write(ch);
                    count++;
                }
                data = bo.toByteArray();

                String output = new String(data);
                JSONObject obj;

                try {

                    obj = new JSONObject(output);
                    JSONObject err = obj.getJSONArray("errors")
                            .getJSONObject(0);
                    x.setMessage(err.getString("message"));
                } catch (JSONException e) {
                    Log.e("stuff", output);
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // Damn you android! I'm using a REST service here, stop
                // trying to interpret my errors!
                x.setMessage("Unable to retrieve message");
            }
            return x;
        }
    } catch (Exception x) {
        x.printStackTrace();
        /*
         * if (!retried && x.toString().equals(
         * "java.io.IOException: Persistent connection dropped after first chunk sent, cannot retry"
         * )) { retry = true; } if (!retry) { return x; }
         */
        return x;
    } finally {
        try {
            out.close();
        } catch (Exception x) {

        }
        try {
            is.close();
        } catch (Exception x) {

        }
        try {
            c.close();
        } catch (Exception x) {

        }
        params.clear();

    }

    // return null;
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:18:11+00:00Added an answer on June 14, 2026 at 1:18 pm

    After a very long time of frustration, we discovered that Android tries to keep a connection alive even if you manually call .close() on the connection. This worked fine for our GET methods, but POST methods left the socket in a state that it couldn’t then process a GET. Adding the following fixed all our problems:

    urlConnection.setRequestProperty("connection", "close");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting some weird behavior in asp.net MVC2 (at least it's not what I'm
I'm getting a very strange behavior in MySQL, which looks like some kind of
I've been debugging some app lately with valgrind, and I'm getting very weird reports
Very weird problem, some RowKeys looks like they are getting 'locked' after some time.
I have a very simple MySQL table with some very weird behavior. Incidentally the
I have a file that I am getting a very weird error on. The
I'm getting some very wierd errors. The compiler seems to want to call the
I'm getting some very strange behaviour using the following switch statement: string recognise_mti(int mti_code)
I am looking for some advice in regards to getting a very quick display
I am getting some weird distortion of my CCLabelBMFont labels in Cocos2D, as noted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.