我使用nodejs作爲服務器和java(android)作爲客戶端,我成功地通過發佈數據從android到節點。但我的問題時,Android的發送數據(串)由空間和新線(輸入)其收到的節點,但性格在變化,通過帖子從android發送數據到node.js?
例如,我在Android
發送此字符串Hello
I learn android
串發送到節點和接收,但是我得到這個節點
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());
}
}
,我用這個代碼領取串節點
req.addListener('data', function(chunk) { data += chunk; });
req.addListener('end', function() {
console.log("from android :"+data); //result of data is Hello%0AI+learn+android
});
我如何解決我的問題?
請幫忙,謝謝。
你檢查值在「文字」中?它的價值是什麼? – Sree
是的,我做的,價值是你好%0AI +學習+機器人,因爲我編碼文本到UTF – ltvie
你需要res.on(「數據」... –