【发布时间】:2018-10-05 18:59:30
【问题描述】:
生成 JSON 对象并调用 Async 方法的代码:
final String OWNER_PASSWORD="ownerPassword";
final String OWNER_EMAIL="ownerEmailId";
private void CheckPassword() {
shopkeeperJSON = new JSONObject();
try{
shopkeeperJSON.put(OWNER_EMAIL,"varuncr7raj@gmail.com");
shopkeeperJSON.put(OWNER_PASSWORD,"1");
} catch (JSONException e) {
e.printStackTrace();
}
new POST_Request(this).execute("http://192.168.1.5:3000/api/CheckPassword", shopkeeperJSON.toString());
}
这是发送 JSON 对象到我的本地服务器的 post 方法的 Android 代码:
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod(REQUEST_METHOD);
httpCon.setRequestProperty("Accept","application/json");
//httpCon.setRequestProperty("Content-Type", "application/json");
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//Input
OutputStream os = httpCon.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(params[1]);
//osw.write("{ ownerEmailId : varuncr7raj@gmail.com, ownerPassword: 1 }");
osw.flush();
osw.close();
os.close(); //don't forget to close the OutputStream
httpCon.connect();
//read the inputstream and print it
String result;
BufferedInputStream bis = new BufferedInputStream(httpCon.getInputStream());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result2 = bis.read();
while(result2 != -1) {
buf.write((byte) result2);
result2 = bis.read();
}
result = buf.toString();
//System.out.println(result);
Log.e("result", result);
httpCon.disconnect();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
这是我的节点 js 服务器的代码。这只是记录请求的 res.body。
exports.checkPassword = function(req,res){
var data = JSON.stringify(req.body);
//console.log(JSON.stringify(req.body));
var obj = JSON.parse(data);
console.log(obj);
res.send(req.body);
};
代码的输出:-
{ '{"ownerEmailId":"varuncr7raj@gmail.com","ownerPassword":"1"}': '' }
如您所见,JSON 对象正在成为 JSON 对象的 JSON 键。谢谢!任何帮助将不胜感激!
期望的输出:
{"ownerEmailId":"varuncr7raj@gmail.com","ownerPassword":"1"}
【问题讨论】:
-
您还没有真正提出明确的问题。您刚刚发布了代码(这是一个好的开始)。结果我真的不知道你需要什么。
-
对不起!问题已更新为所需的输出!谢谢!!
标签: android arrays json node.js