Concatenating a string variable to a url properly - java

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

Related

how to pass text by adding double quotes before and after in android

I want to pass some text by adding double quotes before and after.
I have a retrieved some text(url) from my json array like https://xyzabc and i have stored it in a string.
now i have to add some certificate and some response to that url and trying to retrieve some another data from it(new URL).
Here Iam trying to pass like String newUrl = oldurl+response+certificate
And i have to pass the url in double quotes(must in double quotes) if iam passing like String newUrl = "oldurl+response+certificate", the total is considering as one string so i have to append double quotes before and after the newUrl string without make it as a single String.
I have tried some "\"newUrl""\ but it doent work for me...Suggest me some solution.
Try this:
newUrl = "\""+oldurl+response+certificate+"\"";
Hope it helps:)
Use temporary special character in string literal and convert it into quotation like
String newUrl = ("$"+oldurl+response+certificate+"$").replaceAll('$','"');
You can use regular expression -
String str="\"yourstring here\""

Special characters getting converted into ascii

I need to pass a string as it is but at the back end the special characters are getting converted.How to avoid this? Please find below my code how I am doing it.
My String "1nIH6iLxXVYBj0J\/JhDlQmSm9aAtjz7ynZpaJ4bxcko="
At the backen "1nIH6iLxXVYBj0J%5C%2FJhDlQmSm9aAtjz7ynZpaJ4bxcko%3D"
Part of my code::
oauthMessage = computeOAuthRequest("POST", appId, appSecret,URLEncoder.encode(authToken,"UTF-8"), url);

How to put the string in the JSON and get the value in Java

I want to get the value of String and I am trying to put the string in the JSON and get it. The format of string is "key=value&key1=value". The code I am using is:
JSONObject json = new JSONObject(String);
String value = json.get("key").toString();
That string is url encoded not json formatted.
You can use a url decoder instead. Something like http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html might work for you.
I used Patterns and Matchers and it worked perfectly. Thank you.

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.

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