How to use DefaultHttpClient in Android? [closed]
Asked Answered
W

3

7

How to use DefaultHttpClient in Android?

Whatever answered 23/3, 2011 at 19:51 Comment(2)
you could have put a little more energy to the question....Gulp
It seems like this question could stand to be a bit more specific?Costumier
B
15

I suggest reading the tutorials provided with android-api.

Here is some random example which uses DefaultHttpClient, found by simple text-search in examples-folder.

EDIT: The sample-source was not intended to show something. It just requested the content of the url and stored it as string. Here is an example which shows what it loaded (as long as it is string-data, like an html-, css- or javascript-file):

main.xml

  <?xml version="1.0" encoding="utf-8"?>
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/textview"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
  />

in onCreate of your app add:

  // Create client and set our specific user-agent string
  HttpClient client = new DefaultHttpClient();
  HttpGet request = new HttpGet("http://stackoverflow.com/opensearch.xml");
  request.setHeader("User-Agent", "set your desired User-Agent");

  try {
      HttpResponse response = client.execute(request);

      // Check if server response is valid
      StatusLine status = response.getStatusLine();
      if (status.getStatusCode() != 200) {
          throw new IOException("Invalid response from server: " + status.toString());
      }

      // Pull content stream from response
      HttpEntity entity = response.getEntity();
      InputStream inputStream = entity.getContent();

      ByteArrayOutputStream content = new ByteArrayOutputStream();

      // Read response into a buffered stream
      int readBytes = 0;
      byte[] sBuffer = new byte[512];
      while ((readBytes = inputStream.read(sBuffer)) != -1) {
          content.write(sBuffer, 0, readBytes);
      }

      // Return result from buffered stream
      String dataAsString = new String(content.toByteArray());

      TextView tv;
      tv = (TextView) findViewById(R.id.textview);
      tv.setText(dataAsString);

  } catch (IOException e) {
     Log.d("error", e.getLocalizedMessage());
  }

This example now loads the content of the given url (the OpenSearchDescription for stackoverflow in the example) and writes the received data in an TextView.

Bosporus answered 23/3, 2011 at 19:59 Comment(2)
When i implementing this code the output show me nothing. Plz sir kindly gives me the complete code to access data from url using httpclientWhatever
I changed the example so that it shows the received data in an TextView.Bosporus
G
3

Here is a general code example:

DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

HttpGet method = new HttpGet(new URI("http://foo.com"));
HttpResponse response = defaultHttpClient.execute(method);
InputStream data = response.getEntity().getContent();
//Now we use the input stream remember to close it ....
Gulp answered 23/3, 2011 at 20:4 Comment(0)
D
0

From Google Documentation

public DefaultHttpClient (ClientConnectionManager conman, HttpParams params)

Creates a new HTTP client from parameters and a connection manager.

Parameters
"conman" the connection manager,
"params" the parameters

public DefaultHttpClient (HttpParams params)
public DefaultHttpClient ()
Dong answered 23/3, 2011 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.