I have created an android app. It sends a data message on a port for communicating with the same app on some other phone. While sending the message, i have encoded it into binary data using ISO8859_1 encoding.
byte[] b1=payload.getbytes();
I am able to receive the data message at the receiving end. But the problem is that after receving it in binary format , My app needs to decode the message back to string or human read-able format. But i am not able to do the same.
I have tried to convert it into String using 'toString()' but string contains binary character .
pls help.
Try this:
try {
String s = new String(b1, "ISO8859_1");
} catch (UnsupportedEncodingException e) {
// ...
}
Related
I created webapp which sends file by FileRepresentation. Client is an Android app. How can I get File from Restlet Response object on the client side?
The file content will be present within the payload. So you can extract it like any payload with Restlet, as described below:
ClientResource cr = new ClientResource(...);
Representation rep = cr.get();
In fact, the FileRepresentation class is provided in order to fill request / response from a file but can't be used to extract content of a response.
To have access to your response content on the client side, it depends on the file type. If you receive an ascii content, you can do something like that:
Representation representation = cr.get();
String fileContent = representation.getText();
If it's a binary file, you need to work with a stream, as described below:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
cr.get().write(outputStream);
byte[] fileContent = outputStream.toByteArray();
Hope it helps you,
Thierry
I'm encountering an odd situation whereby strings that I load from my resource XML file that have Spanish characters in them display correctly in my TextViews, but strings that I'm fetching from a JSON file that I load via HTTP at runtime display the missing char [] boxes
ESPAÑOL for example, when embedded in my XML strings works fine, but when pulled from my JSON is rendered as SPAÃ[]OL, so the Ñ is transformed into a à and a missing char!
I'm not sure at what point I need to intercept these strings and set the correct encoding on them. The JSON text file itself is generated on the server via Node, so, I'm not entirely sure if that's the point at which I should be encoding it, or if I should be encoding the fileReader on the Android side, or perhaps setting the TextView itself to be of some special encoding type (I'm unaware that this is an option, just sort of throwing my hands in the air, really).
[EDIT]
As per ianhanniballake's suggestion I am logging and seeing that the screwy characters are actually showing up in the log as well. However, when I look at the JSON file with a text viewer on the Android file system (it's sitting on the SDCARD) it appears correct.
So, it turned out that the text file was, indeed, encoded correctly and the issue was that I wasn't setting UTF-8 as my encoding on the FileInputStream...
The solution is to read the file thusly:
static String readInput() {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream("myfile.json");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
We have a web application made in MVC3 which is hosted in Android & Iphone also .Now the android developer is trying to upload an audio file through our method which is written in controller as http post method . They say they are sending the file in form of multipart chunks or byte buffer body . So I took variable of type byte array[] in my method but the type is not coinciding with their datatype . I even tried to keep string , byte , httppostedfilebase types , but still no success . Any help would be appreciated .
Take a look at the VoiceModel open source project for an example of how to save audio using ASP.NET MVC. There is an example project in there called RecordingExample which takes an audio file sent from a VoiceXML browser or Interactive Voice Response (IVR) systems and saves it to the file system. Here is the code that receives the audio file and saves it to disk.
[HttpPost]
public ActionResult SaveRecording(HttpPostedFileBase CallersMessage)
{
if (CallersMessage != null && CallersMessage.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(CallersMessage.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath(recordingPath), fileName);
_log.Debug("Received recording and will save as " + path);
CallersMessage.SaveAs(path);
}
string vm_id = Request.QueryString["vm_id"];
string vm_event = Request.QueryString["vm_event"];
string vm_result = "";
return VoiceView(vm_id, vm_event, vm_result);
}
My android application receive binary data as UDP packet , how should I convert that in android to ASCII ?
Try the following:
byte [] data = (however you extract data from your source);
String result = new String(data, "iso-8859-1");
Hello I am trying to read a UTF-8 encoded txt files with Hebrew chars on my android application, and now after managing doing for some reason the 'a' char is always appended at the beginning of the String i read.. and I wonder why
Here is my code:
void Read(){
try {
File fileDir = new File("/sdcard/test.txt");
BufferedReader in = new BufferedReader( new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
Log.i("TEST",str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
this is the result i get
05-15 01:53:25.269: INFO/TEST(16236): אבגדהוזחטיכלמנסעפצקרשתa
In order to get a better answer, I need two questions answered:
What is the exact code point of the character in question (your "a")?
What is the exact byte sequence in your file, around the questionable area?
I'm going to take a guess here: You say the character is the first thing in the file ("appended at the beginning of the String") and that you got back it's in the Arabic Presentation Forms B block. The last character of Arabic Presentation Forms B, which oddly has nothing to do with Arabic, is U+FFEF, or the byte order mark (BOM). It usually appears at the beginning of UTF-16 or UTF-32 encoded files, and identifies the "endianess" of the encoding (whether the file is UTF-16LE or UTF-16BE encoded, likewise for UTF-32). It typically does not appear, however, in UTF-8 data, as UTF-8 has no notion of "byte order". That said, some brain-dead Windows programs will stick it there, and then have an additional option of "UTF-8 without BOM". (The BOM is used then to identify a file as likely being encoded in UTF-8.) My guess is you have a BOM in your data, and your program is reading it and passing it on to you.
IF this is your problem, and your file is genuinely encoded in UTF-8, you should be able to find the following byte sequence near the beginning of the file: EF BB BF — this is the UTF-8 representation of U+FFEF.