what is stringentity in android and its use
Solution 1
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();
}
Solution 2
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
.
LMK
Hi Folks, I am a Learner want to improve my skills and help others to learn. I have 5+ year experience in Java. I have basic to intermediate knowledge in Android.
Updated on July 26, 2022Comments
-
LMK almost 2 years
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()
-
LMK about 10 yearshow do i get the data in the servlet/jsp ? Should i use getStream() or request.getParameter()
-
LMK about 10 yearshow do i get the data in the servlet/jsp ? Should i use getStream() or request.getParameter()
-
Bhanu Sharma about 10 yearswhat u want to do dude
-
Bhanu Sharma about 10 yearslike u want to get response from server through any API
-
LMK about 10 yearsas i am sending name,country and twitter as json(from android) in the StringEntity, now in the Serverside , how do i get the data(name,country,twitter) please see the code at 3rd point
-
Bhanu Sharma about 10 yearssee now its my post request method which i use
-
LMK about 10 yearsat response = httpclient.execute(httpPost); you are sending data to server, how do i get that data in Servlet ?
-
LMK about 10 years
-
Bhanu Sharma about 10 yearsServlet i dont knw dude sorry :)
-
ross studtman about 10 yearsservlet has doPost(...request ...response) method. If receiving text data you could create a BufferedReader from request.getReader() inside that method. Then use while-loop and readLine(), etc.
-
Venky over 9 yearsone upvote for convincing answer ...my doubt is then why there is setContentType in stringEntity.we can simply set the content type to http request right ! can you kindly explain me ?
-
Ian Campbell over 9 years@BhanuSharma Thanks for this! So when passing a
StringEntity
viaHttpPost
, should the web-method have a typeString
parameter? Or should it have a typeObject
parameter and cast it toString
? (I am trying to call an ASP.NET .asmx web-service from Android) -
Bhanu Sharma over 9 years@lancampbell Yes u have to pass string in it. public class StringEntity extends AbstractHttpEntity implements Cloneable java.lang.Object ↳ org.apache.http.entity.AbstractHttpEntity ↳ org.apache.http.entity.StringEntity Known Direct Subclasses UrlEncodedFormEntity UrlEncodedFormEntity An entity composed of a list of url-encoded pairs. Class Overview An entity whose content is retrieved from a string. Summary
-
Ramz over 8 yearsStringEntity is depreciated. Please update the code.