Android HTTP發(fā)送請(qǐng)求和接收響應(yīng)的實(shí)例代碼
首先要在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開發(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?AMS啟動(dòng)App進(jìn)程原理分析
這篇文章主要介紹了Android?AMS啟動(dòng)App進(jìn)程原理,系統(tǒng)fork函數(shù)是如何創(chuàng)建進(jìn)程,文中有詳細(xì)的代碼示例,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-05-05
Android?IntentFilter的匹配規(guī)則示例詳解
這篇文章主要為大家介紹了Android?IntentFilter的匹配規(guī)則示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Kotlin語(yǔ)言中CompileSdkVersion與targetSdkVersion的區(qū)別淺析
這篇文章主要介紹了Kotlin語(yǔ)言中CompileSdkVersion和targetSdkVersion有什么區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
Flutter實(shí)現(xiàn)可以縮放拖拽的圖片示例代碼
這篇文章主要給大家介紹了關(guān)于利用Flutter實(shí)現(xiàn)可以縮放拖拽的圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Android富文本實(shí)現(xiàn)的幾種方式匯總
由于項(xiàng)目中需要使用到富文本顯示和編輯,索性整理下,這篇文章主要給大家介紹了關(guān)于Android富文本實(shí)現(xiàn)的幾種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Android用TextView實(shí)現(xiàn)跑馬燈效果代碼
大家好,本篇文章主要講的是Android?TextView實(shí)現(xiàn)跑馬燈效果代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
android通過(guò)藍(lán)牙接收文件打開時(shí)無(wú)法自動(dòng)選擇合適的應(yīng)用程序
android 通過(guò)藍(lán)牙接收文件,從歷史傳輸記錄打開,無(wú)法自動(dòng)選擇合適的應(yīng)用程序,比如video player打開.3gp、.mp4文件等等2013-06-06
Retrofit2.0 實(shí)現(xiàn)圖文(參數(shù)+圖片)上傳方法總結(jié)
本篇文章主要介紹了Retrofit2.0 實(shí)現(xiàn)圖文(參數(shù)+圖片)上傳方法總結(jié),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(五)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第五篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

