Facing special characters on datasource node rendering in place of "-" | Community
Skip to main content
keshava219
New Participant
July 8, 2022
Solved

Facing special characters on datasource node rendering in place of "-"

  • July 8, 2022
  • 5 replies
  • 1661 views

 

 

In above image its rending data from thirdparty api calls by name . in place of   "-"  its coming. 

 

but from api i can see value 

"Welcome to Progress – Image Film".
 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

Making sure you are encoding your strings as UTF-8 in Java should solve the trick. 

 

Encoding With Commons-Codec

 

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.14</version>
</dependency>

String rawString = "Welcome to Progress - Image Film"; 
byte[] bytes = StringUtils.getBytesUtf8(rawString);
 
String utf8EncodedString = StringUtils.newStringUtf8(bytes);


What is UTF-8?

 

UTF-8 is a variable-width character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from Unicode Transformation Format – 8-bit. UTF-8 is capable of encoding all 1,112,064 valid character code points in Unicode using one to four one-byte code units.

5 replies

BrianKasingli
BrianKasingliAccepted solution
New Participant
July 11, 2022

Making sure you are encoding your strings as UTF-8 in Java should solve the trick. 

 

Encoding With Commons-Codec

 

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.14</version>
</dependency>

String rawString = "Welcome to Progress - Image Film"; 
byte[] bytes = StringUtils.getBytesUtf8(rawString);
 
String utf8EncodedString = StringUtils.newStringUtf8(bytes);


What is UTF-8?

 

UTF-8 is a variable-width character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from Unicode Transformation Format – 8-bit. UTF-8 is capable of encoding all 1,112,064 valid character code points in Unicode using one to four one-byte code units.

ShaileshBassi
New Participant
July 8, 2022

@keshava219 Please try out with the below properties in the servlet, while making the request to the api to return the response.

    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("accept-charset", "UTF-8");
    con.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");

 Hope this helps!

Thanks

Manu_Mathew_
New Participant
July 8, 2022

@keshava219  What is your content type? is it "application/x-www-form-urlencoded"?

Aditya_Chabuku
New Participant
July 8, 2022

Hi @keshava219 , This is because of characters encoding/decoding in a API response( as a matter of. fact, any string-based response).

I saw your response is coming from BufferedReader, so now convert BR to String & then pass into below method to get properly decoded values.

please use following code after you receive response & pass each string here to get proper values.

~Aditya.Ch

import java.net.*;
private String allowSplChars(String incomingString) {
        String encodedValue = null;
        try {
        encodedValue= URLEncoder.encode(incomingString.trim(), "UTF-8");
        } catch (UnsupportedEncodingException uee) { 
        	log.error("UnsupportedEncodingException", uee);

        }
         return encodedValue;
	}

 

Thanks,Aditya Chabuku
keshava219
New Participant
July 8, 2022

hi @aditya_chabuku ,

 

 Thanks for the reply, 

 

 

after trying that above method  spaces and also decoder no change previous still same thing coming .

milind_bachani
Employee
July 8, 2022

Hi @keshava219 ,

 

Is character encoding enabled for the third party api response ?

Thanks,

Milind

keshava219
New Participant
July 8, 2022

Hi @milind_bachani ,

 

                              Im not using response to get api details , Directly from getInputStream

here is the snippet im using :

conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod(ABBConstants.GET_REQUEST);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

getting output of json ,

im passing to datasource.