JsonArrayRequest, url parameter maximum size? - android

I'm new to android,
I'm using a Json webservice in my app to update some database fields, but I have an issue that I can't figure out.
With this one it's working :
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"b\"}]";
JsonArrayRequest jor = new JsonArrayRequest(url2, new Response.Listener<JSONArray>().....
Not working with this :
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla bla bla\"}]";
JsonArrayRequest jor = new JsonArrayRequest(url2, new Response.Listener<JSONArray>().....
Could the URL parameter size be the problem ?
Thanks a lot for your help.

Could be a problem with the spaces - try to use %20 instead of a space
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla%20bla%20bla\"}]";
Edit:
There actually is a character limit on GET parameters, but it is about 512, so it shouldn't be a problem here - but you should definitly think of a better solution for longer mails
(Source: Max size of URL parameters in _GET)

Related

Use special characters like 'ß' or space in URL

I get an Address via Geocode which looks like this e.g.: "Downing Street, London" and I build an URL String with this address String and other parameters.
This is how i build the url string:
String url = "http://www.friendlyride.at/...?...&enterstring="+enterstring+"&enterlng="+enterLng+"&enterlat="+enterLat+"&exitstring="+exitstring+"&exitlng="+exitLng+"&exitlat="+exitLat+"&info="+infoinput;
enterstring = "Downing Street, London"
Now when I log the URL (which I then call in a JSON AsyncTask with an HTTPDataHandler), the URL is a link (blue) until an 'ß', space or ',' is in the String.
This is the URL in the Android Monitor (Log.d):
http://www.friendlyride.at/...?...&enterstring=Downing Street, London&enterlng=-0.1272206&enterlat=51.5032077&exitstring=Abbey Rd, London&exitlng=-0.1830032&exitlat=51.5367909&info=info
The whole url should be a link (here it breaks at a space). If I enter the Url manually in the browser, it works with spaces and everything.
So how can i use the whole string as url?
If you need any code, please tell me, I'm not sure what code I should include. :)
Your url needs to be encoded to be valid.
For that purpose and greatly improving your code quality as well, I strongly encourage you using Uri.Builder to build your urls :
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("www.friendlyride.at")
.appendQueryParameter("enterstring", enterstring)
.appendQueryParameter("enterlng", Long.toString(enterLng))
.appendQueryParameter("enterlat", Long.toString(enterLat));
//Append all your other parameters
String myUrl = builder.build().toString();
This will construct a valid encoded url, like : http://www.friendlyride.at?enterstring=Downing%20Street%2C%20London&enterlng=-0.1272206&enterlat=51.5032077
Note : I do not know what the ...?... part ment in your url, I haven't put it in my answer.

MissingFormatArgumentException with square brackets on android

I have this string which I'm trying to format:
String url = "http://api/doSomething.json?params%5Bemail%5D=%s"
String.format(url,email).
The idea is that it ends up looking like this:
http://api/doSomething.json?params[email]=aValue;
I'm currently getting a MissingFormatArgumentException, Format specifier: 5D exception.
Has anyone had issues with this before?
String.format() doesn't like the %5D placeholder - %5D has to be %5d.
Reference: http://developer.android.com/reference/java/util/Formatter.html
... if it was about placeholders.
Anyway, it seems you just want the square brackets.
Therefore, change this
String url = "http://api/doSomething.json?params%5Bemail%5D=%s"
to
String url = "http://api/doSomething.json?params[email]=%s"
In the end i was able to resolve this using a URLEncoder.
This post was particularly helpful -> URL encoding in Android
String queryPart = String.format(PARAM_STRING,
email);
return baseUrl + URLEncoder.encode(queryPart, "utf-8");

Android url in UTF8

I am trying to download image using an url like:
url --> http://www.example.com/path/to/image-ğüçöşı.jpg
InputStream input = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
However in the first line app crashes. Because it has a character like "ı" or "ç". If url doesn't has those character it doesn't crash and works fine.
I could almost say i tried most of the solutions like utf8 encoding and etc, including giving "UTF8" params to HttpClient.
It would be appreciated very much if you could help me. I am looking for any solution that doesn't slow down the code very much.
Thank you
Encode your url or only image name with this code
String query = URLEncoder.encode("strangeChars", "utf-8");

Hostname consist of hyphen is giving IllegalArgumentException in android

I have an url like http://ashok-reddy:8080/hyd which consists hyphen. While making Http Post request, I am getting IllegalArgumentException saying Host name may not be null . I have tried with replacing the hyphen with its hexadecimal value and also tried converting using URLEncoder/Uri.encode(). But nothing has been worked till now.
mHttpPost = new HttpPost("ashok-reddy:8080/hyd");
mEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
mEnvelope.encodingStyle = SoapSerializationEnvelope.ENC;
mStringEntity = new StringEntity(soapData, HTTP.UTF_8);
mStringEntity.setContentType(mContext.getString(R.string.text_xml_content));
mHttpPost.setEntity(mStringEntity);
mHttpResponse = mHttpClient.execute(mHttpPost);
Can anyone please help on this?
Thanks in advance.
Arindam
If you are using a method like URLEncoder, you should not pass the full URL, because it will escape even '//' symbols in url. For example, it will encode :// into %3A%2F%2F
Pass to the function just the parameters list you need to encode to escape special characters.
EDIT:
As I can see you are using: mHttpPost = new HttpPost("ashok-reddy:8080/hyd");
instead of: mHttpPost = new HttpPost("http;//ashok-reddy:8080/hyd");

Size limit of a String while posting to server

I am posting a string to server. If string size is up to 6000KB then its posted successfully. But when size exceeded more than this its showing response -1.
I have tried method of posting: syn_data1 is string . records fetch from data base and then appending to A string builder and finally i create synData1 string from String builder
URL url = new URL(syn_data1);
URLConnection urlc = url.openConnection();
HttpURLConnection huc = (HttpURLConnection)urlc;
huc.setRequestMethod("POST");
huc.setConnectTimeout(3000);
huc.connect();
int response = huc.getResponseCode();
I do care about each special character and remove.But I did not get success
In theory, the URI in an HTTP request can be of any length, but the practical limit is on the order of 2k. Please read here for more info on that.
I am assuming the length is coming from the query string parameters (those name=value pairs that come after the ?). You should be putting these in the POST data, leaving the path part of the URI only. Of course, the server will have to be looking for those parameters in the POST data as well.
Are you passing the NameValue pairs properly . This is one successful way which i use .
List<NameValuePair> loginParams = new ArrayList<NameValuePair>(1);
loginParams.add(new BasicNameValuePair("ColumnName In DB",YourString));
then you do
httppost.setEntity(new UrlEncodedFormEntity(loginParams));
and proceed to execute
It's not clear exactly what you're trying to achieve, but this definitely looks wrong:
URL url = new URL(syn_data1.toString());
URLEncoder.encode(syn_data1.toString(),"UTF-16BE");
If syn_data1 is already a string, you don't need to call toString on it.. and calling URLEncoder.encode doesn't have any side-effects, so the second statement is pointless. Perhaps you want:
URL url = new URL(URLEncoder.encode(syn_data1, "UTF-16BE"));
That's just on the encoding side though - you still shouldn't be trying to use enormous URLs. If you have a lot of data, that should be in the body of the request rather than the URL.

Categories

Resources