Android HTTP实例 发送请求和接收响应
Android Http连接 实例:发送请求和接收响应
添加权限
首先要在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>