Length limit on JSON parameters on GET request - android

My Android app is making two GET calls to my Server API. In the first one, is this, where parameter code is a 256 char String.
$.getJSON( myServerEndpoint, {
action: "doStuff1",
username: $("#username").val(),
code: my256charString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
The second one is this, where parameter code is a 5120 char String. Both reach the same server endpoint.
$.getJSON( myServerEndpoint, {
action: "doStuff2",
username: $("#username").val(),
code: my5120CharString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
When I call both of them from the same device and same user connected to WiFi or most mobile data providers, it works perfectly.
However, when I connect from a Vodafone data connection, the second request never reaches the server. I cannot find any other explanation than that there is a limit on the length of the parameters with Vodafone.
Any ideas or solutions?

OK, so here it goes. First, read this: What is the maximum length of a URL in different browsers?
Yes, there's a limit in the length of the "URL", but someway I don't know how to explain why it is happening only for vodafone. Plus, I don't even know how the request pass through their servers anyways.
As for the solution, you should consider changing from GET request to POST request when the payload is too big.

Quick solution: Base64-encode the code part of the message. Downside: you must decode on the server. This is a standard function in most languages though.
If you're already using Base64 or somesuch cypher, what about Blobs? https://developer.mozilla.org/en-US/docs/Web/API/Blob
chromano's suggestion is spot-on too, just switch to POST and you will definitely get an unlimited Post Body. Downside: Have to JSON.stringify and JSON.parse for yourself, and if you want to expose this URL to a user (say as a link to share) it now can't carry the same information (URL's are GET requests).

Related

How to handle special characters in API URL - Retrofit

I'm using Kotlin and retrofit in my Android App to call API's, but some of the API URLs have these characters in them: ^, #, ~.
I'm using the GET API method. My host URL does not have special / strange characters in them nor does my API method names, but the API methods that I've set up; have a few parameters; and sometimes the arguments I'm sending, when calling the API methods, have these special characters in them.
In those cases I'm not getting a response back and the API call fails.
I've noticed that retrofit changes my URL. It replaces any special character with 3 other characters. I've tested my URLs directly in a browser and in Postman, and they work fine.
Is there something specific I have to do in Kotlin to make retrofit be OK with my special characters?
try this
import java.net.URLEncoder
fun main(args: Array<String>) {
val url = "http://foo bar/"
println(URLEncoder.encode(url, "utf-8")) // note: encodes space to + not %20
}
Output:
http%3A%2F%2Ffoo+bar%2F
You should avoid Special character in Api
URL encoding is often required to convert special characters (such as "/", "&", "#", ...), because special characters:
1. Have special meaning in some contexts, or
2. Are not a valid character for an URL, or
3. could be altered during the transfer.
For instance, the "#" character needs to be encoded because it has a special meaning of that of an HTML anchor.
The character needs to be encoded because it is not a valid URL character. Also, some characters, such as "~" might not transport properly across the internet. Instead of proceeding with the complex process you should focus on correcting the old one.
More you can read here.

Android, Json modifying element

I have a json file on server:
{"images":[
{"url":"...", "likes":"123"},
{"url":"...", "likes":"234"},
{"url":"...", "likes":"345"}
]}
I get the json file on android read it, but if someone likes a picture i want to change the value of the first picture from 123 to 124, is this possible and how can i do this?
The whole point is to change a json value on server,from client side.
Also if this isn't possible how can i make this happen?
Also if i want to get the Top50 rated pictures,how can i sort them and get only the 50 picture,without getting all pictures and sorting them on android ?
Which one is better,initializing the Top50 images,when the user starts the app,or when the user click on the button Top50.I assume that if its when he click the button,there might be some performance issues server side?
My other idea is to have a function server side,which every 10 min,executes automatically and gets the Top50 rated and makes something like json file.So it all happens server side automatically.
To make this happen, client should expose some interface, i.e. function that will allow to modify file on server side. The interface and implementation of this function greatly depends on server itself, i.e. which protocols it handles, what built-in or external modules it supports, which languages are supported, etc... For example, the classic scenario is using apache as HTTP server, CGI enabled, and write CGI function in perl. So, in this case interface would look like http://server.name/like.cgi?image=image123.
How to modify the values on the server ?
For this every like of a photo should be a post request of this sort.
{
"data": [
{
"image_id": 3133456,
"likes": 343
},
{
"image_id": 3133456,
"likes": 343
}
]
}
On parsing this request server updates the corresponding image's like on the server.
How to get the top 50 rated/liked images from the server ?
Again you send a get request to such a url
http://server.getsomething.com/getTop50Images
On server side
On receiving such a request you make a query on the table in your database something like this
select image_id , image_url, likes from image_table limit 50 ORDER BY likes ASC
Now getting those query results and returning them as a json would not be a performance hit until you have huge bulk of data. like some million rows may be in your database.
Response can be something like this
{
"result": [
{
"image_id": 3133456,
"likes": 34400,
"url": "http://flickr.com/someimage"
},
{
"image_id": 3133456,
"likes": 34380,
"url": "http://flickr.com/someimage"
}
]
}
You still avoid using a database yourself but can lease it from clouds services like parse.
However if you won't be using those services then you can take a look at ftp packages for js. Like the neo JavaScript library by apache.
But still a good choice will be to go with the database approach (is quiet simpler).

How to get the particular value from the response message and assign it to another variable in the next request in Jmeter?

I am trying to make a performance testing process by using the Jmeter for the mobile chat application. The scenario i am trying is,need to analyze the output during the N number of new user registration process.I am feeding the N user data through "CSV Data Set Config".In that mentioned the variable names as "phone,ime".
For each new user registration process,the application will generate the one time password when calling the API1.The question is,I need to get that generated one time password from the API1 response message for each phone user and need to assign the value to the variable ${code} when calling the API2.
The below are the API & parameters details:
API1:/api/users/registration-sms.html?
1) Name:phone, Value: ${phone}
2) Name:ime, Value: ${ime}
Example :
Request - POST data: phone=917010370002 & ime=e78b56418b55b32c
Response:{"response":{"httpCode":200,"Message":"True","Code":"5858"}}
API2: /api/users/registration-sms-verfy.html?
1) Name:phone, Value:${phone}
2) Name:code, Value:${code}
Note : For this variable ${code},We need to get the data from the API1("Code":"5858") response message for the respective phone(917010370002).
Please provide me the feasible solution to sort out of my requirement.
Thanks in advance.
You can use Regular extractor post processor like,
Assuming your response is like
{"response":{"httpCode":200,"Message":"True","Code":"5858"}}
your regular expression will be like to extract code value but for specifiic phone no you need to modify it a little bit,
"Code"\:"(\d+)"
for occurance detail you can provide which match no. you want.
Refer Regular Expression Exctrator Post Processor

Ajax post data in android java

I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.
var postTo = 'xyz.php';
$.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
function(data) {
if(data.success){
window.localStorage["sa_id"] = data.mid;
window.location="getempdb.html";
}
if(data.message) {
$('#output').html(data.message);
} else {
$('#output').html('Could not connect');
}
},'json');
I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.
Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.
This is created by using a List of NameValuePair.
from the parameters to the $.post:
{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }
You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.
It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)
In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.

Android: Parsing URL into web service without space character

All I want to do is to send a URL String into my RESTFUL web service with some kind of code like this
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText());
Its all going well until I found error with space character in my URL. I found some solution about replacing the space character with %20 which is I already defined with something like this :
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText().replace(" ", "%20"));
Everything, again, seems going well until i found that the replace(Char oldChara, Char newChara) function can only replace ONE space character, and not two.
For brief example when I send the CharSequence.getText() with values "We won" there will be no error, but when I change the values into "We won the battles" there will be an error issuing that there are some illegal character sent to my RESTFUL web service.
Any kind of answer will come up with my great thanks and big salute
~Regards~
Use replaceAll instead of replace.
Although, you should really be doing proper URL encoding. You can use URLEncoder.encode
for example.

Categories

Resources