Pergunta

I use nodejs as server and java(android) as client,i succes send data through post from android to node. but my problem when android send the data (string) consist of space and new line (enter) its received on node but the character was change,

for example,i send this string from android

Hello
I learn android

the string send to node and received,but i get this in node

Hello%0AI+learn+android

I use this code for send string to node in android.

 public void btnOnClick(){
      String text= URLEncoder.encode(editText.getText().toString(), "utf-8"); //I get from editText and convert to utf-8
      sendToNode(text);

}


public void sendToNode(String text){
    try {                       

                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://myDomain.com:8888/");
                 UrlEncodedFormEntity form;
                 try {
                     Log.i("kirim ke node isitextAsli ",text);
                     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


                     nameValuePairs.add(new BasicNameValuePair("datanah",text));
                     form=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
                     httppost.setEntity(form);

                     HttpResponse response = httpclient.execute(httppost);


                     Log.i("HTTP Post", "Response from server node = " + response.getStatusLine().getReasonPhrase() + "  Code = " + response.getStatusLine().getStatusCode());
                 } catch (ClientProtocolException e) {
                     Log.e("HTTP Post", "Protocol error = " + e.toString());
                 } catch (IOException e) {
                     Log.e("HTTP Post", "IO error = " + e.toString());
       }
}

and I use this code for receive string in node

req.addListener('data', function(chunk) { data += chunk; });
req.addListener('end', function() {
    console.log("from android :"+data); //result of data is Hello%0AI+learn+android 

});

How i solve my problem?

please help,Thanks.

Foi útil?

Solução

The string is URL-encoded, as you explicitly asked for in your code (and need for a regular POST). Decode it on the server.

To decode it on the server side, do:

var querystring = require('querystring'); 
querystring.unescape(data.replace(/\+/g, " "));

Outras dicas

The following is the sample of encoding and decoding, YOU WANT TO DECODE IN THE SERVER PART

        String encoded;  
        try {  
            encoded = URLEncoder.encode(input, "UTF-8");  

        System.out.println("URL-encoded by client with UTF-8: " + encoded);  

        String incorrectDecoded = URLDecoder.decode(encoded, "ISO-8859-1");  
        System.out.println("Then URL-decoded by server with ISO-8859-1: " + incorrectDecoded);  

        String correctDecoded = URLDecoder.decode(encoded, "UTF-8");  
        System.out.println("Server should URL-decode with UTF-8: " + correctDecoded);  
        } catch (UnsupportedEncodingException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top