0

I want to perform following task :

1 > accept input from user and save it

2 > Send this input value with whole json object to server

This is log cat result

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
kirti
  • 183
  • 16

2 Answers2

0

see this

http://hmkcode.com/android-send-json-data-to-server/

How to send a JSON object over Request with Android?

Community
  • 1
  • 1
keshav
  • 3,235
  • 1
  • 16
  • 22
  • i want to send request to Server eg Flight search on particular time duration and place and get that record in listview – kirti Dec 25 '13 at 05:36
0

USe The following code..

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("name", person.getName());
        jsonObject.accumulate("country", person.getCountry());
        jsonObject.accumulate("twitter", person.getTwitter());

        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person); 

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
PankajSharma
  • 1,529
  • 14
  • 27