我的backendcode即node.js正常运行我已经通过使用postman以json格式发送一些数据来检查它们并且它们存储在我的db(mongodb)中,现在我想从edittextfield android获取数据并发送它我写的代码是命中服务器,但我没有得到数据。我已经检查了console.log(req)它没有包含我发送的数据 我之前看到过与之相关的帖子,但它并没有帮助我
这里是android代码
private static String url_create_product =
"http://192.168.1.3:3000/contactlist";
protected String doInBackground(String... args) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
Log.d("params",params.toString());
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(i);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
这里是node.js代码
app.post('/contactlist', function (req, res) {
console.log("i am going to insert");
console.log(req);
console.log(req.body);
console.log(req.query);
console.log(req.params);
db.users.insert(req.body, function(err, doc) {
res.json(doc);
console.log(doc)
});
});
这是错误 the errors that i am getting
makehttprequest方法代码是 public JSONObject makeHttpRequest(String url,String method, 列表参数){
try {// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
服务器错误
Error: invalid json
at parse (E:\app10 - Copy\node_modules\body- parser\lib\types\json.js:79:15)
at E:\app10 - Copy\node_modules\body-parser\lib\read.js:102:18
at done (E:\app10 - Copy\node_modules\body-parser\node_modules\raw-body\index.js:248:14)
at IncomingMessage.onEnd (E:\app10 - Copy\node_modules\body-parser\node_modules\raw-body\index.js:294:7)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
android错误
7292-7312/com.example.malli.login2 E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xb9ac4360
7292-8793/com.example.malli.login2 E/JSON Parser﹕ Error parsing data org.json.JSONException: Value Error of type java.lang.String cannot be converted to JSONObject
答案 0 :(得分:0)
试试这个
JSONObject params= new JSONObject();
try {
params.put("name", name);
params.put("email", email);
params.put("password", password);
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new StringEntity(params.toString()));
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
Toast.makeText(this, params.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
//Other code
服务器上console.log(req.body);
的输出是什么?