bharath
bharath

Reputation: 953

Ajax post data in android java

I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.

var postTo = 'xyz.php';
 $.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
 function(data) {

 if(data.success){
 window.localStorage["sa_id"] = data.mid;
 window.location="getempdb.html";
 }

 if(data.message) {
 $('#output').html(data.message);
 } else {
 $('#output').html('Could not connect');
 }
 },'json');

I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.

Upvotes: 0

Views: 646

Answers (1)

njzk2
njzk2

Reputation: 39405

Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.

This is created by using a List of NameValuePair.

from the parameters to the $.post:

{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }

You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.

It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)

In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.

Upvotes: 1

Related Questions