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

  • Home
  • SEARCH
  • 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 8876589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:14:48+00:00 2026-06-14T19:14:48+00:00

My Android code sends data all the time and it seems to be working

  • 0

My Android code sends data all the time and it seems to be working until a totally unexpected IOException is thrown, freezing everything. I don’t understand why I can send several post requests without problem until the Exception appears, almost randomly. Here’s my code:

public class HttpsClient {

private final static String TAG = "HttpsClient";

private final static String TOKEN_HEADER_KEY = "Token";

private final String urlString;

private SSLContext sslContext;

// application-specific HTTP header
private String TokenHeaderValue = null;



public HttpsClient(String host, String path) {
    // this.sslContext will be init'ed in open()
    this.urlString = "https://" + host + ":443/" + path;
}


public boolean open() {
    try {
        this.sslContext = SSLContext.getInstance("TLS");
        this.sslContext.init(null, null, new java.security.SecureRandom());
        return true;
    } catch (NoSuchAlgorithmException e) {
        Logger.e(TAG, "NoSuchAlgorithmException:");
    } catch (KeyManagementException e) {
        Logger.e(TAG, "KeyManagementException:");
    }

    return false;
}


public byte[] send(byte[] req) {

    Logger.d(TAG, "sending " + Utils.byteArrayToString(req) + " to " + this.urlString);

    URL url;
    try {
        url = new URL(this.urlString);
    } catch (MalformedURLException e) {
        Logger.e(TAG, "MalformedURLException:");
        return null;
    }
    HttpsURLConnection connection;
    try {
        connection = (HttpsURLConnection) url.openConnection();
    } catch (IOException e) {
        Logger.e(TAG, "send IOException 1 " + ((null == e.getMessage()) ? e.getMessage() : ""));
        e.printStackTrace();
        return null;
    }

    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestProperty("Connection", "close");
    try {
        connection.setRequestMethod("POST");

    } catch (ProtocolException ignored) { }
    connection.setSSLSocketFactory(this.sslContext.getSocketFactory());
    connection.setReadTimeout(3000);



    if ( this.TokenHeaderValue != null )
        connection.setRequestProperty(TOKEN_HEADER_KEY, this.TokenHeaderValue);



    {
        final Map<String, List<String>> requestProps = connection.getRequestProperties();
        Logger.d(TAG, requestProps.size() + " Request header(s):");
        for (Map.Entry<String, List<String>> entry : requestProps.entrySet())
            for (String value : entry.getValue())
                Logger.d(TAG, " " + entry.getKey() + ": <" + value + ">");
    }

    try {
        // open up the output stream of the connection 
        DataOutputStream output = new DataOutputStream(connection.getOutputStream()); 

        // write out the data 
        output.write(req, 0, req.length);
        output.flush();


        Logger.i(TAG, "Response Code: " + connection.getResponseCode());
        Logger.i(TAG, "Response Message: " + connection.getResponseMessage()); 
    } catch (SocketTimeoutException e) {
        Logger.e(TAG, "SocketTimeoutException:" + ((null == e.getMessage()) ? e.getMessage() : ""));
        return null;
    } catch (IOException e) { // FAILS HERE !!!!!!!
        Logger.e(TAG, "send IOException 2 " + ((null == e.getMessage()) ? e.getMessage() : ""));
        return null;
    }


    final Map<String, List<String>> responseHeaderFields = connection.getHeaderFields();
    Logger.d(TAG, responseHeaderFields.size() + " Response header(s):");
    for (Map.Entry<String, List<String>> entry : responseHeaderFields.entrySet()) {
        final String key = entry.getKey();
        if ( (null != key) && key.equals(TOKEN_HEADER_KEY) )
            this.TokenHeaderValue = entry.getValue().get(0);
        for (String value : entry.getValue())
            Logger.d(TAG, " " + key + ": <" + value + ">");
    }


    // read response
    ArrayList<Byte> response = new ArrayList<Byte>();

    try {
        DataInputStream input = new DataInputStream(connection.getInputStream()); 

        // read in each character until end-of-stream is detected
        for( int c = input.read(); c != -1; c = input.read() ) {
            response.add((byte) c);
        }
        Logger.w(TAG, "Https connection is " + connection);
        connection.disconnect();
        Logger.w(TAG, "Https connection is " + connection);
        input.close();

    } catch (IOException e) {
        Logger.e(TAG, "send IOException 3 " + ((null == e.getMessage()) ? e.getMessage() : ""));
        return null;
    }

    if ( 0 == response.size() ) {
        Logger.w(TAG, "response is null");

        return null;
    }

    // else

    byte[] result = new byte[response.size()];
    for (int i = 0; i < result.length; i++)
        result[i] = response.get(i).byteValue();

    Logger.i(TAG, "Response payload: " + Utils.byteArrayToString(result));



    return result;          
}
}

I repeat: code works ’til after many post send, then crashes ’cause of an IO Exception. Moreover, server is fine, there’s something wrong in my code.

Here’s the entire error stack trace:

send IOException 2 

0: org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read_byte(Native Method)

1: org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:783)

2: org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readLine(HttpURLConnectionImpl.java:671)

3: org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readResponseHeaders(HttpURLConnectionImpl.java:699)

4: org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getFromNetwork(HttpURLConnectionImpl.java:1088)

5: org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1041)

6: org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:736)

7: org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:146)

8: com.bitstorms.gui.channels.HttpsClient.send(HttpsClient.java:117)

9: com.bitstorms.gui.apps.Application$MyRunnable.run(Application.java:81)

10: java.lang.Thread.run(Thread.java:1020)
  • 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-14T19:14:49+00:00Added an answer on June 14, 2026 at 7:14 pm

    The suspicious point is that you never properly release streams. For instance, you create DataOutputStream output, use it, but never close. Try to change the code that uses finally to close the stream:

    DataOutputStream output = null;
    try {
        output = new DataOutputStream(connection.getOutputStream());
        ...
    } catch (...) {
        ...
    } finally {
        if (output != null) {
             try {
                 output.close();
             } catch (IOException ignored) {}
        }
    }
    

    The same should be done with DataInputStream input and the connection itself (it should be guaranteedly closed in some global finally section).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created my android code wherein I fetch all the data from facebook
I have an android code which starts vibrating for a random amount of time
I have 2 path objects in my android code.I have tried all the way
I'm an Android newbie and I'm trying to update my (working!) code that uses
I'm working on a program that sends data to a server. The server is
The title pretty much says it all. I'm working on an android project currently
Android Code Style Guide defines Android Code Style Rules. To conform to these rules
From my android code I try with the android browser to access a tomcat
I am writing some android code in preparation for a graphics intensive app I
How can I extract this variable bellow from a website to my android code?

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.