Android 网络:使用URLConnection提交请求,获取html代码加载WebView
作者:互联网
else
{
out.println(“登录失败!”);
}
%>
web.xml :
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns=“http://java.sun.com/xml/ns/javaee”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version=“3.0”>
Android端代码:
===========
GetPostMain.java :
public class GetPostMain extends Activity {
private Button mGetBtn, mPostBtn;
private WebView mWebView;
/**
* 服务器响应的字符串
*/
private String mResponse;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
// 使用WebView加载服务器响应的HTML代码
mWebView.loadDataWithBaseURL(null, mResponse, "text/html", "utf-8", null);
}
if (msg.what == 0x456) {
// Toast出服务器响应的字符串
Toast.makeText(GetPostMain.this, mResponse, Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
mGetBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
mResponse = GetPostUtil.sendGet(
"http://192.168.1.102:8080/abc/a.jsp"
, null);
Log.d("1111",mResponse);
// 发送消息通知UI线程更新UI组件
handler.sendEmptyMessage(0x123);
}
}.start();
}
});
mPostBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
mResponse = GetPostUtil.sendPost(
"http://192.168.1.102:8080/abc/login.jsp"
, "username=zhuanghongji&password=123");
Log.d("1111",mResponse);
// 发送消息通知UI线程更新UI组件
handler.sendEmptyMessage(0x456);
}
}.start();
}
});
}
/**
* 初始化控件
*/
private void initViews() {
mGetBtn = (Button) findViewById(R.id.get);
mPostBtn = (Button) findViewById(R.id.post);
mWebView = (WebView) findViewById(R.id.webview);
}
}
工具类 GetPostUtil.java :
public class GetPostUtil
{
/**
* 向指定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;
try
{
String urlName = url + "?" + params;
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
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)");
/*
* 建立实际的连接
* 如果需要发送POST请求,则需要获取URLConnection的OutputStream
* 然后再向网络中输出请求参数
*/
conn.connect();
// 获取所有响应头字段
Map<String, List<String>> map = conn.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet())
{
System.out.println(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 ex)
{
ex.printStackTrace();
}
}
return result;
}
/**
* 向指定URL发送POST方法的请求
* @param 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)");
//向网络中输出请求参数
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
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)
标签:URLConnection,String,URL,html,new,Android,null,conn,请求 来源: https://blog.csdn.net/m0_64604636/article/details/122103564