Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is stringentity in android and its use

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()

like image 344
LMK Avatar asked Mar 05 '14 07:03

LMK


2 Answers

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();

}
like image 151
Bhanu Sharma Avatar answered Sep 29 '22 19:09

Bhanu Sharma


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.

like image 26
vipul mittal Avatar answered Sep 29 '22 19:09

vipul mittal