Java 通过post将数据从android发送到node.js?
我使用nodejs作为服务器,java(android)作为客户端,通过post成功地将数据从android发送到节点。但是我的问题是,android发送的数据(字符串)由空格和新行(回车)组成,它在节点上收到,但字符已更改 例如,我从android发送这个字符串Java 通过post将数据从android发送到node.js?,java,android,node.js,http-post,Java,Android,Node.js,Http Post,我使用nodejs作为服务器,java(android)作为客户端,通过post成功地将数据从android发送到节点。但是我的问题是,android发送的数据(字符串)由空格和新行(回车)组成,它在节点上收到,但字符已更改 例如,我从android发送这个字符串 Hello I learn android 字符串send to node和received,但我在node中得到了它 Hello%0AI+learn+android 我使用此代码在android中将字符串发送到节点 publi
Hello
I learn android
字符串send to node和received,但我在node中得到了它
Hello%0AI+learn+android
我使用此代码在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());
}
}
我如何解决我的问题
请帮助,谢谢。字符串是URL编码的,正如您在代码中明确要求的那样(并且需要定期发布)。在服务器上解码 要在服务器端对其进行解码,请执行以下操作:
var querystring = require('querystring');
querystring.unescape(data.replace(/\+/g, " "));
下面是编码和解码的示例,您希望在服务器部分进行解码
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();
}
是否检查“文本”中的值?它的值是什么?是的,我知道,值是Hello%0AI+learn+android,因为我将文本编码为utfyou need res.on(“数据”…如何在服务器中解码?我尝试过使用文本手动代码。替换(/++/,“”)它不起作用。Node.js有一个内置函数来做这件事。请尝试
var querystring=require('querystring'))
querystring.unescape(数据);
@FMontano:我尝试过在某些情况下工作,只有新行(enter)可以工作,但空格仍然是“+”而不是“@ltvie,尝试querystring.unescape(data.replace(/\+/g)”;
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();
}