I used this code to send post data to server using android. Could anyone give me any idea or your sample how to send POST OR GET json data to server TOMCAT..!
Steps tips :
- create HttpClient
- make POST request to the given URL
- build jsonObject
- convert JSONObject to JSON to String
- set json to StringEntity
- set httpPost Entity
- Set some headers to inform server about the type of the content
- Execute POST request to the given URL
- receive response as inputStream
- convert inputstream to string
-
return result
private class HttpAsyncTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { person = new Person(); person.setName(etName.getText().toString()); person.setCountry(etCountry.getText().toString()); person.setTwitter(etTwitter.getText().toString()); return POST(urls[0],person); } // onPostExecute displays the results of the AsyncTask. @Override protected void onPostExecute(String result) { Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show(); } }
Best Answer
You are using HttpClient. Actually Android deprecates HttpClient insted of HttpClient you need to "HttpURLConnection" for POST Request check Here one of the sample : here
Updated:
You need to add following two more line for the sample code for achieve your requirement.
In given Sample Code: