Search code examples
androidhttpandroid-2.3-gingerbread

Mange HTTP response in android


i am firing a HTTP post request post request from a AsyncTask in android to the server.

the problem i am facing in managing the response. here how i read the response.

HttpEntity entity =response.getEntity();
if (entity != null) {

    DataInputStream in = new DataInputStream( entity.getContent());
    String str;
    while (( str = in.readLine()) != null){


    Log.e("Debug","Server Response second url"+str); 

 }
   in.close();
}

the issue is i am getting the response in pieces , but i wanted them in a one piece

Current Response

 Server Response second url
 Server Response second url<header><status>Success</status><message>Check the Numbers:
 Server Response second url123456.</message><vbal>5005</vbal></header>

Expected Response

<header><status>Success</status><message>Check the Numbers:123456.</message><vbal>5005</vbal></header>

if i will get it in a one piece i can parse the xml response easily and fetch the required value.


Solution

  • This should work for you:

    StringBuilder sb = new StringBuilder();
    String str;
    while ((str = in.readLine()) != null) {
        sb.append(str);
    }
    in.close();
    str = sb.toString(); //here you have the string you need.
    Log.e("Debug","Server Response second url" + str);