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

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.

Related

Convert Xamarin code to Native

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);

Restlet Reference decoding "#" sign

I have setup a URI as below:
router.attach("/pmap/campaign/{campaign}/staffCat/{staffCat}/isp/{isp}", PMAPResource.class);
In the PMAPResource.class I have the following code:
public Representation represent()
{
String campaignID = (String) this.getRequestAttributes().get("campaign");
String staffCat = Reference.decode((String) this.getRequestAttributes().get("staffCat"));
String ispID = (String) this.getRequestAttributes().get("isp");
}
The staffCat field is manual input from user, it can be anything. Some examples are:
BAA2(A)
BAB1#(A)
BA B1
It works for most cases until it hits the # sign where it returns 404 Not Found error. Console dump shows the following /fwd-PMAP/pmap/campaign/1/staffCat/BAB1.
What should I do in order to read the "#" so I can get BAB1#(A) as it is?
# has special meaning in a URL. It must be escaped.
There are actually many special characters in URLs, so you should always escape arbitrary text when building a URL in the client.
In this particular case, the correct URL sent by the client would be something like:
/fwd-PMAP/pmap/campaign/1/staffCat/BAB1%23(A)/isp/XXX
Again, this is a problem that needs to be fixed on the client. The server will decode the %23 for you.

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);

Multiple words not getting searched , not taking space

when i pass string with space in bw the words to the servlet and run the android aaplication
error comes like this
03-01 09:32:41.110: E/Excepiton(1301): java.io.FileNotFoundException: http//address of server:8088/First/MyServlet?ads_title=test test&city=Pune
here ads_title=test test and city = Delhi
but it works fine when i pass single word string
like ads_title=test
and city = Delhi
but when i run query on sql with both the value that works that means query is fine.
String stringURL="http//laddress of server:8088/First/MyServlet" +
String.format("?ads_title=%s&city=%s",editText1.getText(),City);
that is where i am passing the values
Data sent as a URL must be "encoded" to ensure that all the data passes properly to the server to be interpreted correctly. Fortunately, Java provides a standard class URLEncoder and the encoding specified by the World Wide Web Consortium is "UTF-8 so, use
String finalURL = URLEncoder(stringURL,"UTF-8");
(That way you don't have to know what the encoding is for each special character.)
I agree with the comments (not sure why they didn't post as an answer though?) - you want to try encoding your URL - so that the space is handled correctly (%20)
Java URL encoding of query string parameters

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