Convert Xamarin code to Native - java

webView.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/content/{0}", WebUtility.UrlEncode("program.pdf"))));
Does someone know the equivalent of that code?
I tried
webView.loadUrl(String.format("file:///android_asset/pdfjs/web/viewer.html?file={0}", String.format("file:///android_asset/content/{0}", URLEncoder.encode("program.pdf","utf-8"))));
But i guess it's wrong because it is not doing what it's supposed to do

In Java, String.format would use %s for string values in place of those {0} values
Its not really clear what benefits you get from URL encoding program.pdf since it's already a valid URL format for a filename, so it's not necessary, but I'll assume that's just an example.
So you're probably looking for
String filename = "program.pdf";
String url = String.format("file:///android_asset/pdfjs/web/viewer.html?file=file:///android_asset/content/%s", URLEncoder.encode(filename ,"utf-8"));
// TODO: print the url here
webView.loadUrl(url);

Related

Concatenating a string variable to a url properly

This is a follow up question of Removing linebreak from php json output.I couldn't find out what was making that problem but i somehow got rid of value <br ...JSONException.
The Issue
when i use
String url = "http://192.168.32.1/Aafois/notice.php?isBatch=2010&section1='IT'";
I get what i want i.e parsing the JSON to my android app.However when i use
String URL="http://192.168.32.1/Aafois/notice.php?isBatch="+isbatch+"&section1="+"'"+section1+"'";
I get Value of java.lang.string can't be converted to JSONArray...JSONException.So obviously there must be some problem there in this previous line.isbatch is an integer variable and secton1 is a string variable which is URL encoded to utf-8.
P.S
I need single quote ' before and after the variable section1 as the url goes like
http://192.168.32.1/Aafois/notice.php?isBatch=2010&section1='IT'.
I think of your variables is a JSONArray. To concatenate everything, it tries to turn your String into a JSONArray, which is not possible as your string is not JSON but part of a URL.
I checked for the variables isBatch and section1.They were returning null.Modified what was needed.Worked as expected.No more JSONException.
I solved the issue with this
String batch= "C";
String section = "2";
String URL = "http://192.168.32.1/Aafois/notice.php?isBatch=";
URL= URL.concat(batch+"& section ="+section);

Android/java: String url does not work in HttpGet(url)

I construct a string with a value I get from another activity:
Bundle b = getIntent().getExtras();
value = b.getString("bundledata");
url = "http://dl.dropbox.com/u/xxx/apptextfiles/";
url += value;
url += ".txt";
so, the string url looks like http://dl.dropbox.com/u/xxx/apptextfiles/LFC2.txt
Later on I try to read the textfile with HttpGet request = new HttpGet(url) - and the app crash. The strange thing is that if I write url = "http://dl.dropbox.com/u/xxx/apptextfiles/LFC2.txt" it works fine, but not if I construct it like above. Actually, if I take the url value from eclipse and put it in a browser it changes to http://dl.dropbox.com/u/xxx/apptextfiles/%EF%BB%BFLFC2.txt - why?? It look like som strange encoding issue? The string value I get from the other activity is also taken from an online textfile. Anyone has a clue about how to solve this?
Take a look at http://www.w3schools.com/tags/ref_urlencode.asp
It looks like the value field you have in your url string contains special characters -- ï , » and ¿
Thats the reason for your url getting encoded as you referenced. Use a hexeditor or something to make sure your textfile does not contain special characters.

How to encode URL to avoid special characters in Java? [duplicate]

This question already has answers here:
HTTP URL Address Encoding in Java
(24 answers)
Closed 5 years ago.
i need java code to encode URL to avoid special characters such as spaces and % and & ...etc
URL construction is tricky because different parts of the URL have different rules for what characters are allowed: for example, the plus sign is reserved in the query component of a URL because it represents a space, but in the path component of the URL, a plus sign has no special meaning and spaces are encoded as "%20".
RFC 2396 explains (in section 2.4.2) that a complete URL is always in its encoded form: you take the strings for the individual components (scheme, authority, path, etc.), encode each according to its own rules, and then combine them into the complete URL string. Trying to build a complete unencoded URL string and then encode it separately leads to subtle bugs, like spaces in the path being incorrectly changed to plus signs (which an RFC-compliant server will interpret as real plus signs, not encoded spaces).
In Java, the correct way to build a URL is with the URI class. Use one of the multi-argument constructors that takes the URL components as separate strings, and it'll escape each component correctly according to that component's rules. The toASCIIString() method gives you a properly-escaped and encoded string that you can send to a server. To decode a URL, construct a URI object using the single-string constructor and then use the accessor methods (such as getPath()) to retrieve the decoded components.
Don't use the URLEncoder class! Despite the name, that class actually does HTML form encoding, not URL encoding. It's not correct to concatenate unencoded strings to make an "unencoded" URL and then pass it through a URLEncoder. Doing so will result in problems (particularly the aforementioned one regarding spaces and plus signs in the path).
I also spent quite some time with this issue, so that's my solution:
String urlString2Decode = "http://www.test.com/äüö/path with blanks/";
String decodedURL = URLDecoder.decode(urlString2Decode, "UTF-8");
URL url = new URL(decodedURL);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
String decodedURLAsString = uri.toASCIIString();
If you don't want to do it manually use Apache Commons - Codec library. The class you are looking at is: org.apache.commons.codec.net.URLCodec
String final url = "http://www.google.com?...."
String final urlSafe = org.apache.commons.codec.net.URLCodec.encode(url);
Here is my solution which is pretty easy:
Instead of encoding the url itself i encoded the parameters that I was passing because the parameter was user input and the user could input any unexpected string of special characters so this worked for me fine :)
String review="User input"; /*USER INPUT AS STRING THAT WILL BE PASSED AS PARAMTER TO URL*/
try {
review = URLEncoder.encode(review,"utf-8");
review = review.replace(" " , "+");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String URL = "www.test.com/test.php"+"?user_review="+review;
I would echo what Wyzard wrote but add that:
for query parameters, HTML encoding is often exactly what the server is expecting; outside these, it is correct that URLEncoder should not be used
the most recent URI spec is RFC 3986, so you should refer to that as a primary source
I wrote a blog post a while back about this subject: Java: safe character handling and URL building

What is the most efficient way to format UTF-8 strings in java?

I am doing the following:
String url = String.format(WEBSERVICE_WITH_CITYSTATE, cityName, stateName);
String urlUtf8 = new String(url.getBytes(), "UTF8");
Log.d(TAG, "URL: [" + urlUtf8 + "]");
Reader reader = WebService.queryApi(url);
The output that I am looking for is essentially to get the city name with blanks (e.g., "Overland Park") to be formatted as Overland%20Park.
Is it this the best way?
Assuming you are actually wanting to encode your string for use in a URL (ie, "Overland Park" can also be formatted as "Overland+Park") you want URLEncoder.encode(url, "UTF-8"). Other unsafe characters will be converted to the %xx format you are asking for.
The simple answer is to use URLEncoder.encode(...) as stated by #Recurse. However, if part or all of the URL has already been encoded, then this can lead to double encoding. For example:
http://foo.com/pages/Hello%20There
or
http://foo.com/query?keyword=what%3f
Another concern with URLEncoder.encode(...) is that it doesn't understand that certain characters should be escaped in some contexts and not others. So for example, a '?' in a query parameter should be escaped, but the '?' that marks the start of the "query part" should not be escaped.
I think that safer way to add missing escapes would be the following:
String safeURI = new URI(url).toASCIIString();
However, I haven't tested this ...

Escaping & in a URL

I am using jsps and in my url I have a value for a variable like say "L & T". Now when I try to retrieve the value for it by using request.getParameter I get only "L". It recognizes "&" as a separator and thus it is not getting considered as a whole string.
How do I solve this problem?
java.net.URLEncoder.encode("L & T", "utf8")
this outputs the URL-encoded, which is fine as a GET parameter:
L+%26+T
A literal ampersand in a URL should be encoded as: %26
// Your URL
http://www.example.com?a=l&t
// Encoded
http://www.example.com?a=l%26t
You need to "URL encode" the parameters to avoid this problem. The format of the URL query string is:
...?<name>=<value>&<name>=<value>&<etc>
All <name>s and <value>s need to be URL encoded, which basically means transforming all the characters that could be interpreted wrongly (like the &) into %-escaped values. See this page for more information:
http://www.w3schools.com/TAGS/ref_urlencode.asp
If you're generating the problem URL with Java, you use this method:
String str = URLEncoder.encode(input, "UTF-8");
Generating the URL elsewhere (some templates or JS or raw markup), you need to fix the problem at the source.
You can use UriUtils#encode(String source, String encoding) from Spring Web. This utility class also provides means for encoding only some parts of the URL, like UriUtils#encodePath.

Categories

Resources