GET , POST 请求的工具类
package com.net.httpurl.utils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* GET , POST 请求的工具类
*/
public class GetPostUtils {
/**
* 向指定url发送 GET方法的请求
*
* @param url 发送请求的 url
* @param params 请求参数 ,格式为 name1 = value1&name2=value2的形式
* @return URL 所代表的远程资源的响应
*/
public static String sendGet(String url, String params) {
String result = "";
BufferedReader in = null;
String urlName = url + "?" + params;
try {
URL realUrl = new URL(urlName);
//打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
//建立实际连接
conn.connect();
//获取所有的响应头字段
Map<String, List<String>> map = conn.getHeaderFields();
//遍历 所有的 响应头字段
for (String key : map.keySet()) {
Log.d("GetPostUtils", key + " ---> " + map.get(key));
}
//定义 BufferedReader 输入流来读取 URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常! " + e);
e.printStackTrace();
}
//使用 finally 关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 向指定URL发送POST方法的请求
*
* @param url url 发送请求的URL
* @param params 请求参数,请求参数应该是name1=value1 & name2=value2的形式。
* @return URL所代表远程资源的响应
*/
public static String sendPost(String url, String params) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
//打开 url之间的连接
URLConnection conn = realUrl.openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
//获取 URLConnection对象输出对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求餐厨
out.print(params);
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
//建立
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
Activity 界面
package com.net.httpurl;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.net.httpurl.utils.GetPostUtils;
/**
* 使用 UrlConnection 提交请求 169.254.214.117
* http://169.254.214.117:8080/abc/a.jsp
*/
public class UrlConnectionActivity extends AppCompatActivity {
Button get, post;
EditText info, et_url;
String respone; //代表服务器响应的信息
String url; //代表输入的 url
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x1234) {
info.setText(respone); //设置 info 组件显示服务器响应
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url_connection);
get = (Button) findViewById(R.id.send_get);
post = (Button) findViewById(R.id.send_post);
info = (EditText) findViewById(R.id.et_info);
et_url = (EditText) findViewById(R.id.et_url);
url = et_url.getText().toString().trim();
initListener();
}
private void initListener() {
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
super.run();
respone = GetPostUtils.sendGet("http://169.254.214.117:8080/abc/a.jsp", null);
//发送消息通知ui线程 ,更新ui界面
mHandler.sendEmptyMessage(0x1234);
}
}.start();
}
});
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
super.run();
respone = GetPostUtils.sendGet("http://169.254.214.117:8080/abc/login.jsp", "name=admin&pass=test");
//发送消息通知ui线程 ,更新ui界面
mHandler.sendEmptyMessage(0x1234);
}
}.start();
}
});
}
}
该程序所发送的post请求和get请求,是向本地局域网内 "http://169.254.214.117:8080/abc/"
应用下两个页面发送,这个应用是部署在本地计算机上的web应用. 这个应用需要部署在Web服务器(比如Tomcat)中才可以使用 ;
单击按钮 get请求按钮 的界面
单击按钮 post请求按钮 的界面,并提交”name=admin&pass=test”请求参数