Android HTTP發(fā)送請(qǐng)求和接收響應(yīng)的實(shí)例代碼
更新時(shí)間:2013年06月19日 17:09:54 作者:
Android HTTP請(qǐng)求和接收響應(yīng)實(shí)例完整的Manifest文件如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助
添加權(quán)限
首先要在manifest中加上訪問(wèn)網(wǎng)絡(luò)的權(quán)限:
<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;// 發(fā)送請(qǐng)求的按鈕
private WebView mWebView = null;// 用于顯示結(jié)果,用載入html字符串的方式顯示響應(yīng)結(jié)果,而不是使用WebView自己的方式加載URL
// 響應(yīng)
private HttpResponse mHttpResponse = null;
// 實(shí)體
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)
{
// 生成一個(gè)請(qǐng)求對(duì)象
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
// 生成一個(gè)Http客戶端對(duì)象
HttpClient httpClient = new DefaultHttpClient();
// 下面使用Http客戶端發(fā)送請(qǐng)求,并獲取響應(yīng)內(nèi)容
InputStream inputStream = null;
try
{
// 發(fā)送請(qǐng)求并獲得響應(yīng)對(duì)象
mHttpResponse = httpClient.execute(httpGet);
// 獲得響應(yīng)的消息實(shí)體
mHttpEntity = mHttpResponse.getEntity();
// 獲取一個(gè)輸入流
inputStream = mHttpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String result = "";
String line = "";
while (null != (line = bufferedReader.readLine()))
{
result += line;
}
// 將結(jié)果打印出來(lái),可以在LogCat查看
System.out.println(result);
// 將內(nèi)容載入WebView顯示
mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
// 直接使用mWebView.loadData(result, "text/html", "utf-8");會(huì)顯示找不到網(wǎng)頁(yè)
// 換成下面的方式可以正常顯示(但是比較寬,拖動(dòng)可見(jiàn)百度logo)
mWebView.loadDataWithBaseURL(null, result, "text/html",
"utf-8", null);
// 直接載入U(xiǎn)RL也可以顯示頁(yè)面(但是此例子主要是為了驗(yàn)證響應(yīng)返回的字符串是否正確,所以不用下面這行代碼)
// mWebView.loadUrl("http://www.baidu.com/");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
};
}
程序運(yùn)行結(jié)果如下:
參考資料
Android開(kāi)發(fā)視頻教程HTTP操作?!猦ttp://www.marsdroid.org
Android Reference: package org.apache.http:
http://developer.android.com/reference/org/apache/http/package-summary.html
首先要在manifest中加上訪問(wèn)網(wǎng)絡(luò)的權(quán)限:
復(fù)制代碼 代碼如下:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
完整的Manifest文件如下:
復(fù)制代碼 代碼如下:
<?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>
布局代碼如下:
復(fù)制代碼 代碼如下:
<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
主要的代碼:
復(fù)制代碼 代碼如下:
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;// 發(fā)送請(qǐng)求的按鈕
private WebView mWebView = null;// 用于顯示結(jié)果,用載入html字符串的方式顯示響應(yīng)結(jié)果,而不是使用WebView自己的方式加載URL
// 響應(yīng)
private HttpResponse mHttpResponse = null;
// 實(shí)體
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)
{
// 生成一個(gè)請(qǐng)求對(duì)象
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
// 生成一個(gè)Http客戶端對(duì)象
HttpClient httpClient = new DefaultHttpClient();
// 下面使用Http客戶端發(fā)送請(qǐng)求,并獲取響應(yīng)內(nèi)容
InputStream inputStream = null;
try
{
// 發(fā)送請(qǐng)求并獲得響應(yīng)對(duì)象
mHttpResponse = httpClient.execute(httpGet);
// 獲得響應(yīng)的消息實(shí)體
mHttpEntity = mHttpResponse.getEntity();
// 獲取一個(gè)輸入流
inputStream = mHttpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String result = "";
String line = "";
while (null != (line = bufferedReader.readLine()))
{
result += line;
}
// 將結(jié)果打印出來(lái),可以在LogCat查看
System.out.println(result);
// 將內(nèi)容載入WebView顯示
mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
// 直接使用mWebView.loadData(result, "text/html", "utf-8");會(huì)顯示找不到網(wǎng)頁(yè)
// 換成下面的方式可以正常顯示(但是比較寬,拖動(dòng)可見(jiàn)百度logo)
mWebView.loadDataWithBaseURL(null, result, "text/html",
"utf-8", null);
// 直接載入U(xiǎn)RL也可以顯示頁(yè)面(但是此例子主要是為了驗(yàn)證響應(yīng)返回的字符串是否正確,所以不用下面這行代碼)
// mWebView.loadUrl("http://www.baidu.com/");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
};
}
程序運(yùn)行結(jié)果如下:

參考資料
Android開(kāi)發(fā)視頻教程HTTP操作?!猦ttp://www.marsdroid.org
Android Reference: package org.apache.http:
http://developer.android.com/reference/org/apache/http/package-summary.html
您可能感興趣的文章:
- 詳解AngularJS用Interceptors來(lái)統(tǒng)一處理HTTP請(qǐng)求和響應(yīng)
- angular 用攔截器統(tǒng)一處理http請(qǐng)求和響應(yīng)的方法
- Node.js發(fā)送HTTP客戶端請(qǐng)求并顯示響應(yīng)結(jié)果的方法示例
- python通過(guò)get,post方式發(fā)送http請(qǐng)求和接收http響應(yīng)的方法
- JAVA發(fā)送HTTP請(qǐng)求,返回HTTP響應(yīng)內(nèi)容,應(yīng)用及實(shí)例代碼
- 詳解HTTP請(qǐng)求與響應(yīng)基礎(chǔ)及實(shí)例
相關(guān)文章
Android組件化工具ARouter使用方法詳細(xì)分析
這篇文章主要介紹了Android組件化工具ARouter使用方法,組件化項(xiàng)目存在各個(gè)模塊之間耦合,通信麻煩的問(wèn)題,為了解決這個(gè)問(wèn)題,阿里巴巴的開(kāi)發(fā)者就搞出了Arouter這個(gè)框架,以解決上述問(wèn)題2022-10-10android實(shí)現(xiàn)點(diǎn)擊按鈕控制圖片切換
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)點(diǎn)擊按鈕控制圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android簡(jiǎn)單修改原有應(yīng)用和添加應(yīng)用的方法
這篇文章主要介紹了Android簡(jiǎn)單修改原有應(yīng)用和添加應(yīng)用的方法,涉及Android工程項(xiàng)目中針對(duì)源碼的修改及資源文件的編譯等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10android開(kāi)發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
本篇文章主要介紹了android開(kāi)發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android設(shè)備與外接U盤(pán)實(shí)現(xiàn)數(shù)據(jù)讀取操作的示例
本篇文章主要介紹了Android設(shè)備與外接U盤(pán)實(shí)現(xiàn)數(shù)據(jù)讀取操作的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android編程實(shí)現(xiàn)檢測(cè)當(dāng)前電源狀態(tài)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)檢測(cè)當(dāng)前電源狀態(tài)的方法,涉及Android針對(duì)當(dāng)前電源的電量、容量、伏數(shù)、溫度等的檢測(cè)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-11-11Android編程實(shí)現(xiàn)泡泡聊天界面實(shí)例詳解(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)泡泡聊天界面,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android泡泡聊天界面的窗體定義與功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11