0

I'm really struggling with encoding strings with Java and PHP.
I use Java to manage posts in Wordpress. For this I created a GUI in JavaFX and extended the REST-API to create SEO-inserts. The Worpdress-database is encoded with utf8_general_ci.
I send the data as JSON-encoded String via Post to the WP-Api. I use org.JSON. for de-/encoding in Java.
I tried to decode the json-string with php,

json_decode($request->get_param('data'), true);

but i get following error:

Malformed UTF-8 characters, possibly incorrectly encoded

So i tried to convert it with php

$s = iconv("Windows-1252", "UTF-8", $s);

It worked but characters like "ä,ö,ü,ß" etc. just appear as ü, Ã,...

Next I tried it to encode it in Java, as described here Converting Java String From/to utf-8

private String convertToUTF8(String s) {
    String out = null;
    try {
        out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
    } catch (java.io.UnsupportedEncodingException e) {
        return null;
    }
    return out;
}

But with that and without "iconv" it fails again.

What am I doing wrong and how can i solve this?

The server runs with php7.0. I use jdk1.8.0_66 for Java.

2
  • Your convertToUTF8 doesn't "convert" to UTF-8 it "converts" from UTF-8 Commented Jan 29, 2017 at 2:56
  • @Tom No, it doesn't do even that. It exports string to "UTF-8" representation and than imports it as if it was "ISO-8859-1", therefore it produces some garbage. Commented Jan 29, 2017 at 2:58

1 Answer 1

1

Assigning an encryption with a String object is a common mistake. Strings are supposed to be an abstraction over such implementation details as encoding. As long as you're only working with Strings, all you need to know is that they're Unicode. The specific encoding comes into play only when you convert String to bytes, which in your case will probably mean in some HTTP client. I don't know what do you use, so I've attached an example for the one from Apache.

public void sendMessage(CloseableHttpClient client, String endpointUrl, String jsonString) throws IOException {
    HttpPost post = new HttpPost(endpointUrl);
    post.addHeader("Content-Type", "application/json; charset=utf-8"); // Apache would probably set it for you, but since I'm not 100% sure, I added it here.
    post.setEntity(new StringEntity(jsonString, ContentType.create("application/json", Consts.UTF_8))); // here the actual conversion takes place and only here do you need to worry about encoding
    try (CloseableHttpResponse response = client.execute(post)) {
        // act on response here
    }
}

Remark 1: This example is just that: an example, meant to illustrate a point. I haven't tested it, and I don't know Apache HTTP client that well, so be careful.

Remark 2: It seems that you can use ContentType APPLICATION_JSON instead of ContentType.create("application/json", Consts.UTF_8) - it uses UTF-8 by default. But in the context of your question this way seems to be clearer.

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

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.