android httpclient 字符编码格式,Android HTTP发送请求和接收响应的实例代码

添加权限

首先要在manifest中加上访问网络的权限:

...

完整的Manifest文件如下:

package="com.example.httpdemo1"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.example.httpdemo1.HttpDemo1Activity"

android:label="@string/app_name" >

布局代码如下:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".HttpDemo1Activity" >

android:id="@+id/myWebTitle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

android:id="@+id/requestBtn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="Send Request" />

android:id="@+id/webview"

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:layout_above="@id/requestBtn"

android:layout_below="@id/myWebTitle" />

activity_http_demo1.xml

主要的代码:

package com.example.httpdemo1;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebView;

import android.widget.Button;

public class HttpDemo1Activity extends Activity

{

private Button mSendReqBtn = null;// 发送请求的按钮

private WebView mWebView = null;// 用于显示结果,用载入html字符串的方式显示响应结果,而不是使用WebView自己的方式加载URL

// 响应

private HttpResponse mHttpResponse = null;

// 实体

private HttpEntity mHttpEntity = null;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_http_demo1);

mSendReqBtn = (Button) findViewById(R.id.requestBtn);

mSendReqBtn.setOnClickListener(mSendClickListener);

mWebView = (WebView) findViewById(R.id.webview);

}

private OnClickListener mSendClickListener = new OnClickListener()

{

@Override

public void onClick(View v)

{

// 生成一个请求对象

HttpGet httpGet = new HttpGet("http://www.baidu.com/");

// 生成一个Http客户端对象

HttpClient httpClient = new DefaultHttpClient();

// 下面使用Http客户端发送请求,并获取响应内容

InputStream inputStream = null;

try

{

// 发送请求并获得响应对象

mHttpResponse = httpClient.execute(httpGet);

// 获得响应的消息实体

mHttpEntity = mHttpResponse.getEntity();

// 获取一个输入流

inputStream = mHttpEntity.getContent();

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(inputStream));

String result = "";

String line = "";

while (null != (line = bufferedReader.readLine()))

{

result += line;

}

// 将结果打印出来,可以在LogCat查看

System.out.println(result);

// 将内容载入WebView显示

mWebView.getSettings().setDefaultTextEncodingName("UTF-8");

// 直接使用mWebView.loadData(result, "text/html", "utf-8");会显示找不到网页

// 换成下面的方式可以正常显示(但是比较宽,拖动可见百度logo)

mWebView.loadDataWithBaseURL(null, result, "text/html",

"utf-8", null);

// 直接载入URL也可以显示页面(但是此例子主要是为了验证响应返回的字符串是否正确,所以不用下面这行代码)

// mWebView.loadUrl("http://www.baidu.com/");

}

catch (Exception e)

{

e.printStackTrace();

}

finally

{

try

{

inputStream.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

};

}

程序运行结果如下:

edcaf5e920cfeb2d33a2f374310f3753.gif 

参考资料

Android开发视频教程HTTP操作。——http://www.marsdroid.org

Android Reference: package org.apache.http:

http://developer.android.com/reference/org/apache/http/package-summary.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值