0

I am trying to send the following parameters to a server through HTTP POST:

["my_session_id","{this=that, foo=bar}"]

But the server is returning a parse error because of the quotes around the hash.

I am trying to remove them with a regex like so:

params.replaceAll("\\\"\\{", "\\{");
params.replaceAll("\\\"\\}", "\\}");

In all honestly I have no idea what I'm doing. Please help.

Thanks in advance!

1
  • 1
    send value as JSONobject because you will get values from jsonobect on service side easy without using replace or split functions Commented Jan 19, 2013 at 4:27

2 Answers 2

2

There's two issues here: First, you're not re-assigning the string. Strings are immutable in Java (cannot be changed), so you must assign the result. Second, you're replacing "} instead of }".

Here's what I used:

String params = "[\"my_session_id\",\"{this=that, foo=bar}\"]";
params = params.replaceAll("\\\"\\{", "\\{");
params = params.replaceAll("\\}\\\"", "\\}");
System.out.println(params);

Which prints out:

["my_session_id",{this=that, foo=bar}]

PS: Bit of advice, use JSON. Android has excellent JSON handling, and it is supported in PHP as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Are the /'s in params created by toString() ?
@TheVasari Those are there to escape the "s when I created the string. In my code, params is initially equal to ["my_session_id","{this=that, foo=bar}"], just like yours.
0

Is there a reason you are using the regular expression replaceAll? Alternatively, you might try:

 String parameters = parameters.replace("{", "\\{").replace("}", "\\}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.