I am new to android , i am following this tutorial, i have found the code below , there he is converting json string to StringEntity. correct me if i am wrong StringEntity is used to pass the data,Headers like Accept,Content-type to Server.
// 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();
.
.
.
and how do i get the data in the servlet/jsp ? Should i use getStream() or request.getParameter()
An entity whose content is retrieved from a string.
StringEntity is the raw data that you send in the request.
Server communicate using JSON, JSON string can be sent via StringEntity and server can get it in the request body, parse it and generate appropriate response.
we set all our unicode style,content type in this only
StringEntity se = new StringEntity(str,"UTF-8");
se.setContentType("application/json");
httpPost.setEntity(se);
For more help u can take reference this http://developer.android.com/reference/org/apache/http/entity/StringEntity.html
As per your requirement I edit this for post method
HttpPost httpPost = new HttpPost(url_src);
HttpParams httpParameters = new BasicHttpParams();
httpclient.setParams(httpParameters);
StringEntity se = new StringEntity(str,"UTF-8");
se.setContentType("application/json");
httpPost.setEntity(se);
try
{
response = httpclient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode==200)
{
entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
System.out.println("The response is" + responseText);
}
else
{
System.out.println("error");;
}
}
catch(Exception e)
{
e.printStackTrace();
}
StringEntity
is the raw data that you send in the request.
Most of the server communicate using JSON, JSON string can be sent via StringEntity and server can get it in the request body, parse it and generate appropriate response.
Accept,Content-type etc. are sent as the header of the request but StringEntity
is content of it.
Header is not passed in StringEntity
.
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