package com.example.twentyfivecode;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Entity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* @author HD
* @date 2015-12-6
* @package_name com.example.twentyfivecode
* @file_name MainActivity.java
*/
public class MainActivity extends Activity {
private Button btnHttpURLGetRequest;
private Button btnHttpURLPostRequest;
private Button btnPostRequest;
private Button btnGetRequest;
private TextView tvResponse;
public static final int SHOW_RESPONSE = 0;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String response = (String) msg.obj;
tvResponse.setText(response);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnHttpURLGetRequest = (Button) findViewById(R.id.btnHttpURLGetRequest);
btnHttpURLPostRequest = (Button) findViewById(R.id.btnHttpURLPostRequest);
btnPostRequest = (Button) findViewById(R.id.btnPostRequest);
btnGetRequest = (Button) findViewById(R.id.btnGetRequest);
tvResponse = (TextView) findViewById(R.id.tvResponse);
btnGetRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithHttpClientWithGET();
}
});
btnHttpURLGetRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithHttpURLGet();
}
});
btnPostRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithHttpClientPOST();
}
});
btnHttpURLPostRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithHttpURLPost();
}
});
}
private void sendRequestWithHttpURLGet() {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://wwww.baidu.com");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(8 * 1000);
conn.setReadTimeout(8 * 1000);
conn.setRequestMethod("GET");
conn.connect();
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
StringBuilder builder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
Message msg = new Message();
msg.what = SHOW_RESPONSE;
msg.obj = builder.toString();
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void sendRequestWithHttpURLPost() {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://www.baidu.com");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setReadTimeout(8 * 1000);
conn.setConnectTimeout(8 * 1000);
conn.setRequestMethod("POST");
DataOutputStream outputStream = new DataOutputStream(conn
.getOutputStream());
outputStream
.writeBytes("username = admin&password = 123456");
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
StringBuilder builder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
Message msg = new Message();
msg.what = SHOW_RESPONSE;
msg.obj = builder.toString();
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void sendRequestWithHttpClientWithGET() {
new Thread(new Runnable() {
@Override
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
try {
HttpResponse httpResponse = client.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
Message msg = new Message();
msg.what = SHOW_RESPONSE;
msg.obj = response;
handler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void sendRequestWithHttpClientPOST() {
new Thread(new Runnable() {
@Override
public void run() {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.baidu.com");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("username", "admin"));
list.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(list, "utf-8");
httpPost.setEntity(entity);
HttpResponse httpResponse = client.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
String response = EntityUtils.toString(httpEntity,
"UTF-8");
Message msg = new Message();
msg.what = SHOW_RESPONSE;
msg.obj = response;
handler.sendMessage(msg);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnGetRequest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="HttpClient的GET方法请求" />
<Button
android:id="@+id/btnPostRequest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="HttpClient的POST方法请求" />
<Button
android:id="@+id/btnHttpURLGetRequest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="HttpURLcon的GET方法请求" />
<Button
android:id="@+id/btnHttpURLPostRequest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="HttpURLcon的POST方法请求" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvResponse"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twentyfivecode"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<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=".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>