Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ambiguous behavior of new line character

I am uploading a large string to web-service. The string contains new line character which is written as "\n". The data looks some 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 is stored in string JsonArrObj. To upload the data/string, i am using the 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};
    for (int i = 0; i < 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 is uploaded successfully. The problem I am facing is: while string is being converted to jsonParams, the "\n" in the string data gets converted to "\\n" as a result, on the server side, it shows a small box in stead of new line.

When I open this string in NOTEPAD application, it displays small boxes. But when I open it in WORDPAD app, text is displayed on a new line. According to me, I might have entered in-correct "content-type" or encoding. Please suggest a solution for the same.

JsonArrObj= URLEncoder.encode(JsonArrObj, "utf-8"); gave error while uploading itself...

The data which is sent in the jsonParams- jsonArrObj finally 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
like image 388
Pallavi Avatar asked Jun 05 '12 10:06

Pallavi


People also ask

What is the character for New line?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

Is New line One a character?

\n is a way of representing a newline character in various languages and programs but as the name suggests, a newline is only stored in a file as a single character.

Why is New line required at end of file?

Because that's how the POSIX standard defines a line: 3.206 Line. A sequence of zero or more non- <newline> characters plus a terminating <newline> character. Therefore, lines not ending in a newline character aren't considered actual lines.

How do you end a file with a new line in Linux?

You need to use the >> to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.


1 Answers

Well, the encoder escapes your newline characters. If you want to transport newline chars properly, you can encode the whole stream with base64. If your target os (for data to send) is Windows then you should use \r\n, if mac then \r if unix\linux then \n. After encoding data try to send the encoded and decode it on the other side. For base64 Mr. Google will convince you.

like image 97
Mert Gülsoy Avatar answered Oct 07 '22 11:10

Mert Gülsoy