Java string encoding(getBytes not working) - java

Now I have a return message in Chinese from API, it used big5 to encode.
Unfortunately, my web page used UTF-8, so it cant't show properly.
I google this question many times, so I tried different getBytes function many times, and I don't want to use file system to handle it as far as possible. So can anyone supply an effective solution to me?
My JDK version is 1.7, and sorry that this version can't change in this project.

You should be using the encoding/decoding methods present in https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

Related

JavaMail not able to display html body

I am having trouble getting the SimpleClient sample app that comes with JavaMail, to work. Emails with plain text bodies display just fine in the viewer. But for html bodies it fails to find the appropriate content viewer. It's the line Object bean = dh.getBean(ci); at around line 205 in MessageViewer.getBodyComponent(), that results in bean getting assigned a null pointer, that causes the problem.
Earlier today I asked for help finding the source for javax.activation, hoping I could figure out the problem. But unfortunately when I step into that code, it goes to the wrong line in the source file, meaning I can't for the life of me figure out what is going wrong!
[I know nothing about Java Beans; this could be my problem. Please feel free to recommend that I do some learning about this technology :-) I haven't yet, for the simple reason that I have other things to do with my time. If, however, it's necessary, then I'll do it :-) ]
Has anyone else had this problem and how did you solve it?
The JavaBeans Activation Framework (JAF) has been included in the JDK since JDK 1.6. You can find the source code in the OpenJDK repository for recent versions.
When JAF was included in the JDK, none of the sample viewers were included. You can download the standalone JAF 1.1.1 release, which includes the sample viewers, and include it in your CLASSPATH.
And, just to be really clear, these are sample viewers, perhaps good enough for the JavaMail sample programs, but not good for much else. In particular, the text viewer displays all text as if it were plain text. You could get the same effect by changing the simple.mailcap file included with the SimpleClient to use "text/*".

Transliteration with Android

I want to transliterate (not translate!) text from arbitrary (as far as possible) languages to English in an Android app. Is there a built-in way?
I've found https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/libcore/icu/Transliterator.java but it doesn't seem to be available by default (at least the IDE doesn't find it). Do I simply need to add this code, as suggested by the comment in Where can I get a JAR to import libcore.io??
Alternately, I could add ICU4J to dependencies and follow icu4j cyrillic to latin. But this is a very large dependency (though Proguard should help).
Finally, I could easily add transliteration from Cyrillic myself and wait until/if someone actually needs other languages (with obvious drawbacks).
Under the hood, Android has all of ICU4J available under android.icu, but only a subset is exposed as public API. If you want to use a class that isn't exposed, you can write code that uses the class and it should work fine. However, doing so is not technically supported, so there could be some version of Android somewhere that doesn't have the class for whatever reason and causes your code to break. (This is unlikely to happen in practice, but possible nonetheless.)
NOTE: The namespace android.icu was added in Android 7 Nougat, so it may or may not be usable yet depending on the version of Android you are targeting.
try compile "org.robovm:robovm-rt:+".
robovm-rt has libcore inside. works for me.

Podio API, attaching files to items

I have a problem with attaching a file to a specific item using Java API. I know it should be possible as this functionality described here in the Podio documentation https://developers.podio.com/doc/files/attach-file-22518 and examples for PHP and Ruby are given. However I cannot find such method in the podio java library. I could find in FileAPI just methods that provide uploading files, but not attaching them to specific objects as described in documentation.
I use Podio APi version 0.7.1
Any ideas how it should be done in Java?
Podio uses a REST-Style API. You send standard http-request, and you get back json-formatted data. So you can do it all without a special library for your programming language.
If there is no predefined java class for you, you can just do the call yourself. In the end it is just a HTTP-call.
From the ruby implemention, I see that you attach the file as multipart/form-data,
so it is the same a browser would do it. There should be http-handling java classes to help you.
You also need to add the information from the API-Page, like the POST-Parameters and of course the url. The most difficult part is probably the authentication headers, but you need to solve this problem only once.

Service similar to Airbrake.io for java applications?

We made our own api for airbrake.io in java. This works fine but airbrake is displaying parameters and stacktraces in some kind of Rails style. This is somewhat annoying. Anyone know of similar services made for java?
Example of how data is displayed:
Parameters
{"controller"=>"", "action"=>""}
Stacktrace
/testapp/app/models/user.rb:53:in `public'
/testapp/app/controllers/users_controller.rb:14:in `index'
UPDATE 2015-02-13: This service no longer exists. The GitHub account linked below is gone, as is the company website.
Have you tried using Coalmine https://github.com/coalmine/coalmine_java Its meant to be used with the Coalmine service: https://getcoalmine.com/
I work at Coalmine and we have been using this internally for some time now. We just open sourced the java connector this week and I would be happy to help you get started with it. You can send me an email at brad#builtfromsource.com
Have you tried using http://code.google.com/p/hoptoad/ . It's a little out of date, but it should just need to update an endpoint to http://api.airbrake.io .
A quick google lead me to http://logdigger.com/ which is designed specific for JAVA specific sites.
I work at Airbrake, and I would be happy to work with you to make our site more JAVA friendly. Please get in touch ben#airbrake.io, and I'll see how we can better display java specific information.
Just adding to the others suggested here, but Raygun (http://raygun.io) has first class support for Java.
Read more here: http://raygun.io/java
I work for Mindscape who built Raygun so can answer any questions you may have about it: jd#mindscape.co.nz. We already have a large number of organizations using Raygun with their Java apps, although Raygun does support other platforms (.NET, Node, Rails, PHP, etc)

Java problems with UTF-8 in different OS

I'm programing with other people an application to college homework, and sometime we use non-english characters in comments or in Strings displayed in the views. The problem is that everyone of use is using a different OS and sometimes different IDE's to program.
Concretely, one is using MacOS, another Windows7, and another and me Ubuntu Linux. Furthermore, all of them use Eclipse and I use gedit. We have no idea if Eclipse or gedit are configurable to work propertly with UTF8 bussiness, at least I don't found nothing for mine.
The fact is that what I write with non-english characters appears in Windows & MacOS virtual machines with strange symbols and vice-versa , and sometimes, what my non-linux friends write provokes compilation warnings like this: warning: unmappable character for encoding UTF8.
Do you have any Idea to solve this? It is not really urgent but it will be a help.
Thank you.
Not sure about gedit, but you can certainly configure eclipse to use whatever encoding you like for source code. It's part of the project properties (and saved in the .settings directory within the project).
Eclipse works fine with UTF-8. See Michael's answer about configuring it. Maybe for Windows and/or MacOS it is really necessary. Ubuntu uses UTF-8 as the default encoding so I don't think it's necessary to configure Eclipse there.
As for Gedit, this picture shows that it is possible to change the encoding when saving a file in Gedit.
Anyway, you need to make sure that all of you use UTF-8 for your sources. This is the only reasonable way to achieve cross-platform portability of your sources.
You could avoid the issue in Strings by using character escape sequences, and using only ASCII encoding for the files.
For example, an en dash can be expressed as "\u2013".
You can quickly search for the Java code for individual characters here.
As Sergey notes below, this works best for small numbers of non-ASCII characters. An alternative is to put all the UTF-8 strings in resource files. Eclipse provides a handy wizard for this.
If your UTF8 file contains a BOM (byte order mark) then you will have a problem. It is a known bug , see here and here.
The BOM is optional with UTF8 and most of the time it is not there because it breaks many tools (like Javadoc, XML parser,...).
More info here.

Categories