android - ambiguous behavior of new line character -
i uploading large string web-service. string contains new line character written "\n". data looks thing like:
05/06/2012 11:35:43 am- db exists, transferring data\n05/06/2012 11:48:20 am- loaduserspinners, cursor.getcount()=2\n05/06/2012 11:48:20 am- battery: 50%\n05/06/2012 11:48:20 am- item selected: 0 the above data stored in string jsonarrobj. upload data/string, using following code:
httpparams httpparameters = new basichttpparams(); int timeoutconnection = 360000; //6 minutes httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection); int timeoutsocket = 420000; //7 minutes httpconnectionparams.setsotimeout(httpparameters, timeoutsocket); httpclient httpclient = new defaulthttpclient(httpparameters); jsonarray jsonparams = new jsonarray(); object[] params={ipaddress,database,dbname,dbpassword,jsonarrobj}; (int = 0; < params.length; i++) { jsonparams.put(params[i]); } jsonobject jsonrequest = new jsonobject(); jsonrequest.put("id", id); jsonrequest.put("method", functionname); jsonrequest.put("params", jsonparams); jsonentity entity = new jsonentity(jsonrequest); entity.setcontenttype("application/json; charset=utf-8"); httppost request = new httppost(url); request.setentity(entity); httpresponse response = httpclient.execute(request); statusline statusline = response.getstatusline(); int statuscode = statusline.getstatuscode(); if (statuscode == 200) { httpentity httpentity = response.getentity(); inputstream content = httpentity.getcontent(); bufferedreader reader = new bufferedreader( new inputstreamreader(content,"iso-8859-1"),8); string line; while ((line = reader.readline()) != null) { builder.append(line); loge("result line: "+line); string str=convertstring(line); parsejson(str); } content.close(); } the string uploaded successfully. problem facing is: while string being converted jsonparams, "\n" in string data gets converted "\\n" result, on server side, shows small box in stead of new line.
when open string in notepad application, displays small boxes. when open in wordpad app, text displayed on new line. according me, might have entered in-correct "content-type" or encoding. please suggest solution same.
jsonarrobj= urlencoder.encode(jsonarrobj, "utf-8"); gave error while uploading itself...
the data sent in jsonparams- jsonarrobj looks like:
05\/06\/2012 04:05:52 pm- db exists, transferring data\\n05\/06\/2012 04:32:56 pm- loaduserspinners, cursor.getcount()\\u003d2\\n05\/06\/2012 04:32:56 pm- battery: 50%\\n05\/06\/2012 04:32:56 pm- item selected: 0
well, encoder escapes newline characters. if want transport newline chars properly, can encode whole stream base64. if target os (for data send) windows should use \r\n, if mac \r if unix\linux \n. after encoding data try send encoded , decode on other side. base64 mr. google convince you.
Comments
Post a Comment