6

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": "example@gmail.com",
    "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": "example@gmail.com",
"phone": "99887766",
"username": "test1234"
}
]
}

On failure: {"error":["Auth error"]}

4

3 回答 3

3

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.

于 2013-10-20T18:42:42.267 回答
2

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\": \"example@gmail.com\"," +
                              "\"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.

于 2013-10-20T19:45:21.700 回答
0
PostMethod method = new PostMethod();
org.apache.commons.httpclient.URI newUri = new org.apache.commons.httpclient.URI(URIstring, true);
method.setURI(newUri);
method.setRequestBody(JsonObj().toString());
method.setRequestHeader("Content-Type", "application/json");
org.apache.commons.httpclient.HttpClient newhttpClient = new org.apache.commons.httpclient.HttpClient();
int statusCode = newhttpClient.executeMethod(method);
return method.getResponseBodyAsString();
于 2013-10-21T12:22:19.507 回答