On an Android device, could an HttpURLConnection recognize the charset of its response automatically?
That is, if I have received some plain text response through an HttpURLConnection, can I get a right String (or maybe a right Reader) without knowing the charset used to encode the response?
You can call getContentType() which returns the responses MIME type. If it's a text-based response then this may include the character set, which you can then extract and pass to an InputStreamReader along with the InputStream you get by calling getInputStream().
If the response is not text-based, i.e. it's binary data, then the concept of charset is meaningless.
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.
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 try to serialize some webservice parameters using Gson and I get a strange error:
11-03 00:56:43.088: **.helpers.JsonServer(620): Result data:
{"Message":"Error during serialization or deserialization using the
JSON JavaScriptSerializer. The length of the string exceeds the value
set on the maxJsonLength property.\r\nParameter name:
input","StackTrace":" at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer
serializer, String input, Type type, Int32 depthLimit)\r\n at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String
input)\r\n at
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext
context, WebServiceMethodData
methodData)","ExceptionType":"System.ArgumentException"}
The parameteres I try to serialize are some strings and numbers, but the issue is when I have a big string (a base64 image) as parameter.
I didn't find a way to increase the depthLimit in Gson object. Any idea?
my code looks like this:
Gson gson = new Gson();
httpPost.setEntity(new StringEntity(gson.toJson(parameters), "utf-8")); //here I get the error
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
parameters is a Hashtable. One of the items in this hash is a big (more than 600k chars) string - the base64 image. This image can be bigger some time - more than 1M.
Question:
How to increase that limit
I just found that the error is not from Android, but from web service. The webservice, .Net, has some limits.
Just in case if someone is interested in how to fix this, set in web.config file:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="999999999"/>
</webServices>
</scripting>
</system.web.extensions>
The Gson works perfect!
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.
I'm using JSON Framework to update the data of client side from server. I have 30 Pojo Classes and i received the Http Response from server. I create the Object of Reader by using the method
InputStream instream = entity.getContent();
reader = new InputStreamReader(instream, "UNICODE");
And i pass it in the Json Object like this
synchronizationResponse = gson.fromJson(reader, SynchronizationResponse.class);
But this line is giving outOfMemory Exception.
I write the response in a file and found the size of file is around 12MB.
So is their any way to split the response in multiple response.So that i can read and write simultaneously to avoid OOM exception.
Looking for Help
Difficult to say, as long as we do not know, what your SynchronisationResponse Class looks like.
Whatever your content, if you can split it into sub-sections, do so and serialize those.
You can use a JsonReader to parse a large stream of JSON without running out of memory. The details are in the documentation, but basically you create a JsonReader:
JsonReader reader = new JsonReader(new InputStreamReader(instream, "UNICODE"));
and write a recursive descent parser using reader.nextName() to get keys, reader.nextString(), reader.nextInt(), etc. to get values, and reader.beginObject(), reader.endObject(), reader.beginArray(), reader.endArray() to mark object boundaries.
This allows you to read and process one piece at a time, at any granularity, without ever holding the entire structure in memory. If the data is long-lived, you can store it in a database or a file.