Android HTTP发送请求和接收响应的实例代码
程序员文章站
2023-08-12 20:33:00
添加权限 首先要在manifest中加*问网络的权限: 复制代码 代码如下:
添加权限
首先要在manifest中加*问网络的权限:
<manifest ... >
<uses-permission android:name="android.permission.internet" />
...
</manifest>
完整的manifest文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpdemo1"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="8"
android:targetsdkversion="17" />
<uses-permission android:name="android.permission.internet" />
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<activity
android:name="com.example.httpdemo1.httpdemo1activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>
布局代码如下:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
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" >
<textview
android:id="@+id/mywebtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<button
android:id="@+id/requestbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignparentbottom="true"
android:text="send request" />
<webview
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@id/requestbtn"
android:layout_below="@id/mywebtitle" />
</relativelayout>
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();
}
}
}
};
}
程序运行结果如下:
参考资料
android开发视频教程http操作。——http://www.marsdroid.org
android reference: package org.apache.http:
http://developer.android.com/reference/org/apache/http/package-summary.html
首先要在manifest中加*问网络的权限:
复制代码 代码如下:
<manifest ... >
<uses-permission android:name="android.permission.internet" />
...
</manifest>
完整的manifest文件如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpdemo1"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="8"
android:targetsdkversion="17" />
<uses-permission android:name="android.permission.internet" />
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<activity
android:name="com.example.httpdemo1.httpdemo1activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>
布局代码如下:
复制代码 代码如下:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
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" >
<textview
android:id="@+id/mywebtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<button
android:id="@+id/requestbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignparentbottom="true"
android:text="send request" />
<webview
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@id/requestbtn"
android:layout_below="@id/mywebtitle" />
</relativelayout>
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();
}
}
}
};
}
程序运行结果如下:
参考资料
android开发视频教程http操作。——http://www.marsdroid.org
android reference: package org.apache.http:
http://developer.android.com/reference/org/apache/http/package-summary.html