what is stringentity in android and its use

21,006

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.

Share:
21,006
LMK
Author by

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, 2022

Comments

  • LMK
    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
    LMK about 10 years
    how do i get the data in the servlet/jsp ? Should i use getStream() or request.getParameter()
  • LMK
    LMK about 10 years
    how do i get the data in the servlet/jsp ? Should i use getStream() or request.getParameter()
  • Bhanu Sharma
    Bhanu Sharma about 10 years
    what u want to do dude
  • Bhanu Sharma
    Bhanu Sharma about 10 years
    like u want to get response from server through any API
  • LMK
    LMK about 10 years
    as 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
    Bhanu Sharma about 10 years
    see now its my post request method which i use
  • LMK
    LMK about 10 years
    at response = httpclient.execute(httpPost); you are sending data to server, how do i get that data in Servlet ?
  • LMK
    LMK about 10 years
  • Bhanu Sharma
    Bhanu Sharma about 10 years
    Servlet i dont knw dude sorry :)
  • ross studtman
    ross studtman about 10 years
    servlet 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
    Venky over 9 years
    one 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
    Ian Campbell over 9 years
    @BhanuSharma Thanks for this! So when passing a StringEntity via HttpPost, should the web-method have a type String parameter? Or should it have a type Object parameter and cast it to String? (I am trying to call an ASP.NET .asmx web-service from Android)
  • Bhanu Sharma
    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
    Ramz over 8 years
    StringEntity is depreciated. Please update the code.