My backendcode i.e node.js is running fine i have checked it by sending some data in json format using postman and they are getting stored in my db(mongodb) and now i want to take the data from edittextfield android and send it to the server.the code that i have written is hitting the server but i am not getting the data. i have checked in console.log(req) it is not contaning the data that i am sending.
And i have seen previous posts that were asked related to this but it didnt helped me
here is the android code
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;
}
here is the node.js code
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)
});
});
here is the error the errors that i am getting
the makehttprequest method code is public JSONObject makeHttpRequest(String url, String method, List params) {
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;
}
server error
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 errors
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