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 8806199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:04:23+00:00 2026-06-14T02:04:23+00:00

I am having a bit of trouble writing an android app that can communicate

  • 0

I am having a bit of trouble writing an android app that can communicate with my server to send and receive data. So the jist of it is that my server is written in Qt C++ and, of course, my app is written in java for android. A lot of complications have arisen from getting the socket data to be in the proper format for both sides to fully understand.

Well the problem I am having is that my client (java android) can connect to the server, send a small bit of data, and receive a response from the server. The server sends a decently large sized message of data converted to UTF-8 bytes.

QByteArray sendblock;
QDataStream out(&sendblock, QIODevice::WriteOnly);
out << (quint16)0;
char * data = msg.toUtf8().data();
out.writeRawData(data, msg.toUtf8().length());
out.device()->seek(0);
out << (quint16)(sendblock.size() - sizeof(quint16));
socket.write(sendblock);
socket.waitForBytesWritten();

The msg is a QString passed to the function.

On the client side, when the message is received, I have:

Log.d("connection", "Waiting for response");
short inSize;
inSize = in.readShort();
Log.d("connection", Integer.toString(inSize)); // the first 2 bytes are the size of the message
byte[] inData = new byte[inSize];//allocate space for the entire message
in.read(inData, 0, inSize); //read the entire message
String resp = new String(inData, 0, inSize, "UTF-8");//cast into a String
Log.d("connection", resp);

So the problem is that on differing instances of my program, the bytes cast to String differ in results.

The results of the Log.d printing the response is roughly 1445 letters (2890 bytes) of the string correctly formatted into a string, then the rest of the String ends up as “??” characters.

Rarely I get the entire message as expected with no “?” characters. Sometimes I get longer lengths of the message correctly cast as a String. Most of the time I just get 1445 of the letters correctly cast and the rest are “??” characters.

I have another client for windows computers (also written in Qt C++) that always gets the message complete and does not have this issue, so I am curious if there’s something with android Strings and encoding that might be causing this problem. At first I thought it might have to do with memory allocation for the Strings, but I am pretty sure I have handled that properly with setting the sizes.

  • 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-14T02:04:24+00:00Added an answer on June 14, 2026 at 2:04 am

    As far as I can tell, the UTF-8 encoding and endian-ness of the byte count look correct. I think the only thing you are missing is you need to do your read in a while loop, since read is not guaranteed to fill the array. Try something like this. Check the return value of read to determine how many bytes are actually read and keep reading until you have ‘inSize’ bytes.

    Log.d("connection", Integer.toString(inSize)); // the first 2 bytes are the size of the message
    byte[] inData = new byte[inSize];//allocate space for the entire message
    
    int count = 0;
    while (count < inSize)
    {
       int len = in.read(inData, count, inSize - count);
       count += len;
    }
    String resp = new String(inData, 0, inSize, "UTF-8");//cast into a String
    Log.d("connection", resp);
    

    You would need to extend this code and handle the case where len is -1 indicating the stream is closed.

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

Sidebar

Related Questions

I'm having a bit of trouble writing a class that uses generics because this
im having a bit of trouble writing a keyboard hook in C++. I can
I'm having a bit of trouble with a C# program I'm writing and it'd
I'm having a bit of trouble with a a function that the mongodb c
I'm having a bit of trouble understanding how to send a 2D array to
I'm having a bit of trouble writing an sql for this scenario. For example
I'm having some logic trouble with a bit of jQuery I'm writing. I suspect
I'm writing a currency converter but I'm having a bit of trouble caculating the
I am having a bit trouble to understand when super can be called and
I am having a bit of trouble with C++ classes. I am writing a

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.