I m new on Android Programmer,i want to send data to server through JSON in following format and implement Json in this format..and also want to fetch data from server..
{
"signup": [
{
"username": "test1264",
"password": "1234",
"email": "[email protected]",
"phoneno": "223344556",
"altphoneno": "12345678",
"firstname": "abc",
"lastname": "xyz"
} ]
}
Response:
on success:
{"status":1}
on failure:{"status":0}
{"login":[
{"username":"test1234",
"password":"1234"}
]}
Response:
on success: {
"user": [
{
"firstname": "abc",
"lastname": "xyz",
"email": "[email protected]",
"phone": "99887766",
"username": "test1234"
}
]
}
On failure: {"error":["Auth error"]}
To post JSON data to the server, we need to use the HTTP POST request method and set the correct MIME type for the body. The correct MIME type for JSON is application/json. In this POST JSON example, the Content-Type: application/json request header specifies the media type for the resource in the body.
To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.
When sending data to a web server, the data has to be a string. So we are using JSON. stringify() function to convert data to string and send it via XHR request to the server.
You can use code like this
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://fort.example.com");
JSONObject json = new JSONObject();
try {
// Add your data
json.put("key", "value");
StringEntity se = new StringEntity( json.toString());
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String jsonString = reader.readLine();
JSONTokener tokener = new JSONTokener(jsonString);
JSONObject finalResult = new JSONObject(tokener);
Don't forget to add this to nonUI thread.
You can use droidQuery to do this very easily:
$.ajax(new AjaxOptions().url("http://fort.example.com")
.type("POST")
.data("\"signup\": [{" +
"\"username\": \"test1264\"," +
"\"password\": \"1234\"," +
"\"email\": \"[email protected]\"," +
"\"phoneno\": \"223344556\"," +
"\"altphoneno\": \"12345678\"," +
"\"firstname\": \"abc\"," +
"\"lastname\": \"xyz\"" +
"\"}]")
.dataType("json")
.success(new Function() {
@Override
public void invoke($ d, Object... args) {
JSONObject json = (JSONObject) args[0];
boolean success = json.getBoolean("status");
if (success) {
//handle success
}
else {
//handle error
}
}
})
.error(new Function() {
@Override
public void invoke($ d, Object... args) {
AjaxError error = (AjaxError) args[0];
Log.i("Ajax", "Error " + error.status + ": " + error.reason);
}
}));
You can repeat this logic for other cases, such as your login web service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With