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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:56:41+00:00 2026-06-16T06:56:41+00:00

I have an Android device that creates a JSON formatted message and sends it

  • 0

I have an Android device that creates a JSON formatted message and sends it to a Tomcat server. The server is running a Java Web Application that receives this JSON formatted message and will then process it.

My issue is that when the message is received by the server, the server replaces certain characters ( ‘{‘, ‘[‘, ‘”‘, ‘]’ and ‘}’ ) with what looks like their Hex representation, which causes a JSON error where the server doesn’t recognize the message.

My question is whether anyone knows what is causing this issue and how I would go about fixing this (I’m certain it’s not the JSON code, but rather way the message is handled in terms of being sent/received).

Here is my server-side code:

@Override
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Setup response
    PrintWriter out = response.getWriter();

    // Store input message as String
    StringBuilder sb = new StringBuilder();
    String message;

    // Build String
    while ((message = request.getReader().readLine()) != null) {
        sb.append(message);
    }

    // Full message as String
    message = sb.toString();

    // Convert message to JSON format
    Gson gson = new Gson();
    ImageFeatures features = gson.fromJson(message, ImageFeatures.class);

    out.println("Normal data: " + features);
    out.println();
}

Here is the error I am receiving:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 12
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
com.google.gson.Gson.fromJson(Gson.java:795)
com.google.gson.Gson.fromJson(Gson.java:761)
com.google.gson.Gson.fromJson(Gson.java:710)
com.google.gson.Gson.fromJson(Gson.java:682)

This is the JSON formatted message that is produced before the Android device sends the message to the server:

{
"image_x_size":800,
"image_y_size":1600,
"features":
    [
        {"cart_x":5.0,"cart_y":124.0,"polar_angle":0.0,"polar_dist":0.0,"size":15.0},
        {"cart_x":5.0,"cart_y":124.0,"polar_angle":0.0,"polar_dist":0.0,"size":15.0}
    ]
}

This is how the message is seen by the server (i.e. the contents of the message String):

%7B+%09%22image_x_size%22%3A800%2C+%09%22image_y_size%22%3A1600%2C+%09%22features%22%3A+%09%09%5B+%09%09%09%7B%22cart_x%22%3A5.0%2C%22cart_y%22%3A124.0%2C%22polar_angle%22%3A0.0%2C%22polar_dist%22%3A0.0%2C%22size%22%3A15.0%7D%2C+%09%09%09%7B%22cart_x%22%3A5.0%2C%22cart_y%22%3A124.0%2C%22polar_angle%22%3A0.0%2C%22polar_dist%22%3A0.0%2C%22size%22%3A15.0%7D+%09%09%5D+%7D+

As you can see, the message String has various hex values replacing certain characters.

Many Thanks in Advance!

EDIT: I thought I should include my Android code for further clarity

HttpClient http_client = new DefaultHttpClient();

// Set long timeout limit
HttpConnectionParams.setConnectionTimeout(http_client.getParams(), 10000);
HttpConnectionParams.setSoTimeout(http_client.getParams(), 10000);
HttpResponse response;

// Setup json_object
JSONObject json_object = convertImageFeaturesToJSON(data, x, y);

try {
HttpPost http_post = new HttpPost(URL_BASE + "/featureuploader");

Log.i(TAG, json_object.toString());

StringEntity se = new StringEntity(json_object.toString());
se.setContentType("application/json; charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset=UTF-8"));

http_post.setEntity(se);
http_post.setHeader("Accept", "application/json");
http_post.setHeader("Content-Type", "application/json");

response = http_client.execute(http_post);

if (response != null) {
    String response_string = httpResponseToString(response.getEntity().getContent());
    if (! response_string.startsWith("Received: <html>")) {
        Log.i(TAG, "Received: ERROR! - " + response_string.substring(0, 20));
    } else {
        Log.i(TAG, "Received: " + response_string);
    }
} else {
    Log.e(TAG, "Did not receive from server");
}               
} catch (Exception e) {
    Log.e(TAG, "Error: " + e.getMessage());
}
  • 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-16T06:56:42+00:00Added an answer on June 16, 2026 at 6:56 am

    This is how the message is seen by the server…

    That’s a URL encoded version of the JSON that you’re seeing on the server. You need to either decode it first (using something like URLDecoder), or retrieve it from the request in a way that it comes to you decoded.

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

Sidebar

Related Questions

I have to Send Byte Array to my java application From Android device.I m
I have an issue that when I leave the android device idle for a
I have the following code running on my Android device. It works great and
I have a Runnable that transfers data(a few hundred files) from an android device
On server we have a set of JSON APIs. There's a login method that
I am going to implement an application that is gathering data from Android device.
I have a Phonegap (2.1.0) application that onDeviceready creates a DB and populates a
I have the following code that supposed to send data from web application developed
I have an Android device that I'm using to monitor a couple of sensors
I have developed a REST based server in java for Android devices and Desktop

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.