Am 26.03.2009 um 16:12 schrieb Stoyan Damov:


ResponseHandler<String> responseHandler = new BasicResponseHandler();
String body = httpClient.execute(method, responseHandler);

That's much simpler. But is there a way to get the status code if you do it this way?

Lutz



2009/3/26 Lutz Schönemann <lutz.schoenem...@sit.fraunhofer.de>:
If you use the DefaultHttpClient to connect to your server you don't need to reconnect. You'll receive a HttpResponse. You should validate the status code if the request was successful. after that just retrieve the HttpEntity. This class provides a getContent() method which returns a InputStream:

--- snip ---

HttpResponse response = null;
InputStream in = null;

try {
client.execute(method);
} catch(Exception e) {
       // handle exception
}

if(response.getStatusLine().getStatusCode() == 200) {
       HttpEntity entity = response.getEntity();
       if(entity != null) {
               try {
                       in = entity.getContent();
               } catch(IOException e) {
                       // handle exception
               }
       }
}

if(in != null) {
       // use in to read body of http response
}

--- snip ---

Maybe there is a better way to get the body from the response but I don't
know it...

Lutz

Am 26.03.2009 um 15:43 schrieb zeeshan:


thanks for the reply, it is solved actually

i was adding header before it get initialised

it should be like this
method = new HttpGet( new URI(url) );
 method.addHeader("Connection","Keep-Alive");

i suppose to get an xml on this request , how can i make input stream?

do i need to connect it again like below

HttpURLConnection httpConnection = (HttpURLConnection)
url.openConnection();

InputStream in = httpConnection.getInputStream();

On Mar 26, 1:15 pm, Stoyan Damov <stoyan.da...@gmail.com> wrote:

What happens if you do this instead:

method.getParams().setParameter("http.useragent", this.user_agent_id);

?

On Thu, Mar 26, 2009 at 2:54 PM, zeeshan <genx...@gmail.com> wrote:

Hi Experts,

i am trying to add header on HttpGet but it raise this exception
"InvocationTargetException"

here is my code:

DefaultHttpClient client = new DefaultHttpClient();
          HttpGet method = null;
          HttpResponse resp = null;

String url = "http://www.x.com/a.xml";;

      method.addHeader("Connection","Keep-Alive");
      /*method.addHeader("accept-language",this.accept_language);
      method.addHeader("user-agent-id",this.user_agent_id);*/

      method = new HttpGet( new URI(url) );

i also used setHeader but same error

please help me out






--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---



Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to