samedi 18 avril 2015

How do i render a data on android from node.js/mongodb server

The post method works successfully, its just that i can't see the string that am sending from android to the server showing in the server


This is my code



protected String doInBackground(String... params) {
postData(params[0]);
return null;
}

@Override
protected void onPostExecute(String s) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Your Post has been successfully posted",
Toast.LENGTH_SHORT).show();
}

public void postData(String titleToSend){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://ift.tt/1HloeL3");

try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("title", titleToSend));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = client.execute(post);
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}


The onClick listener code



public void onClick(View v) {
String tit = null;

if(title.getText().toString().length()<1){
Toast.makeText(getApplicationContext(), "Please Fill in all fields", Toast.LENGTH_SHORT).show();
}else{
pb.setVisibility(View.VISIBLE);
myTask task = new myTask();
task.execute(title.getText().toString());
}
}


The data-type name in the server is title, so what the user input in the EditText should go to the title field.


The backend api.js



api.route('/')
// GET method to get list of ads
.get(function(req, res) {
Ad.find({}, function(err, ads) {
if(err) {
res.send(err);
return;
}
res.json(ads);
});
})

// POST method to post an ad to the database
.post(function(req, res) {
var ad = new Ad({
title: req.body.title,
photos: req.body.photos,
});


Can anyone point to me what am doing wrong here or guide me on hoo should i do it please


Aucun commentaire:

Enregistrer un commentaire