从Android应用程序发送到nodejs服务器通过HTTP发送后,我的JSON看起来不同
当我试图通过HTTP发送一个JSON作为一个string到一个nodejs服务器,当我试图loggingrequest.body它看起来不同,我不能访问任何领域。
我的类发送请求到服务器:
public class SendData extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... strings) { String data = ""; HttpURLConnection httpURLConnection = null; try{ httpURLConnection = (HttpURLConnection)new URL(strings[0]).openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); wr.writeBytes(strings[1]); wr.flush(); wr.close(); InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream()); int inputStreamData = inputStreamReader.read(); while (inputStreamData != -1){ char current = (char) inputStreamData; inputStreamData = inputStreamReader.read(); data += current; } } catch (Exception e){ e.printStackTrace(); } finally { if(httpURLConnection != null){ httpURLConnection.disconnect(); } } return data; } }
服务器代码:
app.post('/', function (req, res) { var a = req.body; console.log(a); res.send(a); })
和JSON看起来像这样:
{ '{"name":"ffffc","email":"asdxx","password":"ffv","tel":"111"}': '' }
但应该看起来像这样:
{"name":"ffffc","email":"asdxx","password":"ffv","tel":"111"}
您的Java代码需要将它的Content-Type设置为application/json
,如下所示:
httpURLConnection.setRequestProperty("Content-Type", "application/json");
- 在Phonegap(Windows)中找不到脚本文件“C:\ res \ windows \ zip.js”
- / data / data /是安装path的可靠假设吗?
- 在android上使用chrome时,获取通知警告证书,Nodejs(expressjs)在端口443(https)上运行
- 如何安装Visual Studio 2015以使用现有的NodeJs,Cordova和android SDK的安装
- 如何添加外部npm包到phonegap?
- Android Volley:POST请求 – NodeJS REST API中的req.body为空
- 套接字Web应用程序 – 处理与移动超时/退出?
- 如何使用android应用程序控制覆盆子pi gpio端口?
- 看守:加载共享库时出错:libpcre.so.1