【问题标题】:How to use DefaultHttpClient in Android? [closed]如何在 Android 中使用 DefaultHttpClient? [关闭]
【发布时间】:2014-07-31 15:35:55
【问题描述】:

如何在Android中使用DefaultHttpClient?

【问题讨论】:

  • 你本可以在这个问题上投入更多精力......
  • 看来这个问题可以更具体一点?

标签: android httpwebrequest apache-httpclient-4.x apache-commons-httpclient


【解决方案1】:

我建议阅读 android-api 提供的教程。

这是一些使用 DefaultHttpClient 的随机示例,通过示例文件夹中的简单文本搜索找到。

编辑:示例源不是为了显示某些内容。它只是请求 url 的内容并将其存储为字符串。这是一个显示加载内容的示例(只要它是字符串数据,如 html-、css- 或 javascript 文件):

ma​​in.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"
  />

在应用的 onCreate 中添加:

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

此示例现在加载给定 url 的内容(示例中为 stackoverflow 的 OpenSearchDescription)并将接收到的数据写入 TextView。

【讨论】:

  • 当我实现这段代码时,输​​出什么也没显示。请先生好心地给我完整的代码来使用 httpclient 从 url 访问数据
  • 我更改了示例,使其在 TextView 中显示接收到的数据。
【解决方案2】:

这是一个通用代码示例:

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 ....

【讨论】:

    【解决方案3】:

    来自Google Documentation

    public DefaultHttpClient (ClientConnectionManager conman, HttpParams params)

    根据参数和连接管理器创建一个新的 HTTP 客户端。

    参数
    "conman"连接管理器,
    "params"参数

    public DefaultHttpClient (HttpParams params)
    public DefaultHttpClient ()
    

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 2012-10-25
      • 2012-09-29
      • 2016-11-18
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多