I am sending json object to server, My url having json object.Json object having special characters,how to encoding special characters in android, please help me.Thanks in advance.
Use UrlEncode.encode(yourstring, "UTF-8");
If I understood you, you want to send a json object using the url itself (that is, as a parameter). If security doesn't matter you (it will be visible), you could just encode using [Base64][1].
You should probably play with your json object to convert it to a byte[], called jsonbytes, for instance, then use Base64.encodeToString(jsonbytes, Base64.URL_SAFE) and sending this as a parameter.
Your server then should convert this Base64 encoded string into a json object, which tends to be straightforward if you're using PHP:
$jsonString = base64_decode($_GET['json']);
$json = json_decode($jsonString, TRUE);
This will give you an associative array in PHP. If you just want the json string, skip the last step.
Related
How can I send special character parameter in URLencoded form without change using volley library
I have to send
qty$1:23
qty$2:666
this data in URLencoded form using volley library but due to Volley Request class it is doing UTF encoding foe key value pairs.How can I avoid to change the parameter names.
Using Volley now parameters names are changed like qty%241:23 which is not acceptable by server.Please help me
Try encoding first:
String encoded = URLEncoder.encode(stringToEncode, "UTF-8"); and send the encoded string.
According to the documentation of this method:
This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.
I'm sending string below from Android to web service using volley request
params.put("data", objData.toString());
objData.toString() -->
{"report_time":1413876429,"device_id":"13d404d1b3a38ffc","ads_info":"[{\"play_count\":\"26\",\"ad_id\":\"21\"},{\"play_count\":\"58\",\"ad_id\":\"37\"},{\"play_count\":\"14\",\"ad_id\":\"40\"}]"}
Server not accepting it due the \"
How to remove \ keeping the json correct ""
You are not using the web service correctly.
Possibly the service expects to see a JSON array instead of a string containing JSON. Assuming params is a JSONObject and objData is a JSONArray, use params.put("data", objData) instead of the toString() version you have now.
Escaping the double quotes with backslashes is the only way to do this in java.
One other option would be to put the String into some kind of text file that you would then read at runtime.
iam calling a webservice(php http get request)
http://website.com/admin/employee_login.php?fun_name=abc&company_code=1&employee_ss=123>ime=02:22 PM&x_id=350&v_id=9&task={"Walking":"1","Transfers":"1","OstomyCare":"1"}
but when am encoding this url am getting something different with %20 something like that, and it's not updating the tasks in the server database. Is there any method where i can pass parameters as a json array without encoding like same above ?
thank you
Before sending the url and parameters are url encoded. For instance spaces become %20. On the receiving side php should url decode the parameters. There is a php function which does that for you. After that you have the original parameters back.
use:
String value = "my url value"; // put here your url
URLEncoder.encode(value,"UTF-8")
I need to decode a json response sent from php in android.
JSON Response
{"VehicleNumber":"1234FB14","Make":"BMW","Model":"X5","Type":"Car","Color":"Black","EngineCapacity":"1800cc","Fuel":"Diesel","DateOfReg":"31-Dec-2013","OwnerNIC":"B1234567890123","serviceArray":[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]}
The response consists of values for their respective keys and I can retrieve these value by just calling getString(key) in java. {key:value,2D Array[][]}
The problem is that I can't decode the 2D Array being passed here. I get it in a string format in java and if I display it in my android app I can view this:
[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]
But i need to retrieve all the value from the 2D array.
Any help please.
If Your json is quite big I recommend using gson or Jackson libraries.
I have to parse this json data. The data begins with [ and ends with ]
How can we parse such json data? json data usually starts with {..[..]..}
Just create a JSONArray from your input. There is even a constructor taking a String as parameter. So, basicly you need to do something like this:
String input = .. //read your input
JSONArray arr = new JSONArray(input);
//work with the array as usual..
take result data as a JSONArray and not a JSONObject.
It depends on how you parse the data. Built in json or google json(gson) etc.
But normally you dont have to care about that it starts with square bracket.
Show me what the json array/object look like and I can give you an example.