URL Decoder not working - android

I am trying to decode a name string which contains apostrophe. This name string is being parsed in an XML parser, so what I want to do is that the apostrophe should be displayed normally, rather than being displayed as '. I tried using the URLDecoder.decode(myString, "UTF-*"); to decode it but to no avail. From one solution posted on StackOverflow I found that we need to use the URLDecoder.decode() twice. However, even that is not working. The code that I am using is:
try {
temp = URLDecoder.decode("Wing's", "UTF-8");
nameOrg = URLDecoder.decode(temp, "UTF-8");
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
At the end it should display Wing's as Wing's, but it is displaying the same string. Any help would be great. Thanks in advance.

Why don't you just decode it using Html.fromHtml(String source),
Spanned temp = Html.fromHtml("Wing's");
Log.d("decoded", temp.toString());
Output - Wing's

Related

how to convert byte string to pdf to display it in Web view in Android Studio?

I am calling a json API from which I am getting a pdf element , it looks like this , this file is only for example as it is corrupted but looks like this :
JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSbDQTRGRTNBQ0MxQTg5PjwxOTIzMDRGMTgwOTZCMzQ0QjExQ0E0RkUzQUNDMUE4OT5dIC9QcmV2IDk3NjgwL1hSZWZTdG0gOTY0MTg+Pg0Kc3RhcnR4cmVmDQoxMDgzNzgNCiUlRU9G
I want to convert it into pdf to display it into web view in my application.
My tried Code:
String fileUrl = response.getPdf();
byte[] bytes = null;
try {
bytes = fileUrl.getBytes("UTF-8");
Log.e("Byte Array",bytes.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
It Converts into [B#a4d4cef. After this i am not understanding what to do.
Any help would be appreciated.
Thanks.

How to Display HTML Text in Android TextView?

I Have an HTML Text.
Sales Resources Drive to Collab Partner PresentationUS-CanadaLATAMEMEARAPJCDrive to Collab Program FAQsPlatform for Voice and Video Campaign
I would like to display this text in Android Text View. I have tried MyTagHandler to display, it is working fine while displaying, but the text formating is not good. If the bulleted text has 2 lines then the second line is coming from first, but i need space upto bullet. how can i handle this?
i guess like this...
TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml("your html text"));
You can do that in 2 ways.
First you create a HTML Script for it.
Once you are done with it, then set that to textview as shown below.
(preferable for smaller text)
myTextView.setText(Html.fromHtml("Html Script"));
Create a html file and put it in res/raw.
Then use the follwoing code.
myTextView.setText(Html.fromHtml(getHtmlText()));
Now define getHtmlText() method as below:
private String getHtmlText(){
InputStream inputStream = getResources().openRawResource(R.raw.my_html_file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}

Open raw text recourse and diplay wrong next line

InputStream inputStream = getResources().openRawResource(idtext);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String myText = "";
int in;
try {
in = inputStream.read();
while (in != -1) {
byteArrayOutputStream.write(in);
in = inputStream.read();
}
inputStream.close();
myText = byteArrayOutputStream.toString();
} catch (IOException e) {
e.printStackTrace();
}
myTextView.setText(myText); `
My code is used to display long text file in raw res. I don't know why, but some of text file display wrong about next line, any help?
It would be good if you can share some sample display vs your expected output.
From the initial guess, it could be because of couple of reasons:
The encoding used in the text file. So, if you have written the text in ASCII and displaying it while using UTF-8 strings, it would mess up few things. This should be consistent.
Could be a good case of how line feeds are encoding in the file, like \r\n or just \n.
You can also try encapsulating your InputStream to FileReader and line reading streams which are more specialized in directly reading strings, rather than converting it.
You can probably use a library like Apache Commons IO to manage all the stuffs for you.

Using image URL which has dashes in it

I'm trying to download images from a website and my code is working fine in most cases, but I can't download from this URL http://www.liveandlocal.org.uk/images/ShowPics/Steiny’s%20Blues%20-%20Such%20Sweet%20Thunder.jpg
The difference between this and the other URLs is that this one has a dash. I'm fairly certain this is the problem. Is there a way around this?
My error is java.io.filenotfoundexception
My initial code was:
imgLink = "http://www.liveandlocal.org.uk/images/ShowPics/" + Show + ".jpg";
imgLink = imgLink.replace(" ", "%20");
This gave me links like: http://www.liveandlocal.org.uk/images/ShowPics/The%20Atlantics.jpg
which works, but this didn't work for the link I posted at the top.
So now I've tried:
try {
Show = URLEncoder.encode(Show, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imgLink = "http://www.liveandlocal.org.uk/images/ShowPics/" + Show + ".jpg";
Which doesnt work for any of my links, such as http://www.liveandlocal.org.uk/images/ShowPics/The+Atlantics.jpg
If you copy and paste the link at the start of this post into your browser it will work, so it is just not working on Android.
imgLink = "http://www.liveandlocal.org.uk/images/ShowPics/Steiny%E2%80%99s%20Blues%20-%20Such%20Sweet%20Thunder.jpg"
working fine for me, tested it, replace the characters thus accordingly and '.
- is fine.
Your url contains letters which are not suited for urls (' in this case).
You can encode your url as the following:
String encodedUrl = URLEncoder.encode(normalUrl, "UTF-8");
URLEncoder

Android: How to read a txt file which contains Chinese characters?

i have a txt file which contains many chinese characters, and the txt file is in the directory res/raw/test.txt. I want to read the file but somehow i can't make the chinese characters display correctly. Here is my code:
try {
InputStream inputstream = getResources().openRawResource(R.raw.test);
BufferedReader bReader = new BufferedReader(
new InputStreamReader(inputstream,Charset.forName("UTF-8")));
String line = null;
while ((line= bReader.readLine())!= null) {
Log.i("lolo", line);
System.out.println("here is some chinese character 这是一些中文字");
}
} catch (IOException e) {
e.printStackTrace();
}
Both Log.i("lolo", line); and System.out.println("here is some chinese character 这是一些中文字") don't show characters correctly, i can not even see the chinese characters in the println() method.
What can i do to fix this problem? Can anybody help me?
In order to correctly handle non-ASCII characters such as UTF-8 multi-byte characters, it's important to understand how these characters are encoded and displayed.
Your console (output screen) may not support the display of non-ASCII characters. If that's the case, your UTF-8 characters will be displayed as garbage. Sometimes, you will be able to change the character encoding on the console. Sometimes not.
Even if the console correctly displayed UTF-8 characters, it's possible that your string does not correctly store the Chinese characters. You may think that it's correct because your editor displays them, but ensure that the character encoding of your editor also supports UTF-8.
I also was trying to figure out that. First you need to open the .txt file with the notepad and then click on File->Save as, there you will see a dropdown menu that says Enconding, so change it to UTF-8. After saving the file you should remove the .txt extension to the file and then add the file to the path res/raw and then you can refer to it from the code as R.raw.txtFileName.
That's all, i will put my code where I used the chinese characters and I could show them in the emulator.
If you have any other question, let me know because i am also developing something related with characters. Here is the code:
public List<String> getWords() {
List<String> contents = new ArrayList<String>();
try {
InputStream inputStream = getResources().openRawResource(R.raw.chardb);
BufferedReader input = new BufferedReader(new InputStreamReader(inputStream,Charset.forName("UTF-8")));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.add(line);
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents;
}

Categories

Resources