<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.marsdroid.http01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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="org.marsdroid.http01.MainActivity"
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>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.marsdroid.http01.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/requestButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="向百度发送一次请求" >
</Button>
</FrameLayout>
package org.marsdroid.http01;
import java.io.BufferedReader;
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.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
/**
* @author YongHeng
*
*/
public class MainActivity extends ActionBarActivity {
private Button requestButton = null;
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestButton = (Button)findViewById(R.id.action_mode_close_button);
requestButton.setOnClickListener(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{
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while((line = reader.readLine()) != null){
result = result +line;
}
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
});
}
}