Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data to server through JSON

Tags:

json

android

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..

  • URL: http://fort.example.com
  • Signup
{

    "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}

  • JSON for user login:
{"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"]}

like image 971
Arpan Dixit Avatar asked Oct 20 '13 18:10

Arpan Dixit


People also ask

How do I POST JSON to the server?

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.

Can I send JSON in GET request?

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.

Can JavaScript send data to server?

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.


2 Answers

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.

like image 88
Vanama Avatar answered Oct 04 '22 22:10

Vanama


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.

like image 28
Phil Avatar answered Oct 04 '22 22:10

Phil