社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
1.URL请求的类别:
分为二类,GET与POST请求。
二者的区别在于:
1:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,
2:) post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
[协议://] <域名|IP地址>[: 端口号] [/资源相对路径][?参数名=参数值][&参数名=参数值]……
Get请求:获取数据时用。(URL的长度是有限制的)
区分是不是get请求,看?那一块(在地址栏看见?就是get请求)
Post请求:放置在HTML Header内提交,URL中看不到数据的内容,比get请求安全,并且不受URL长度的限制(增删改查时用) 在地址栏看不见
HttpActivity代码:
public class HttpActivity extends AppCompatActivity {
private Button search;
private Button search1;
private TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http);
search = (Button) findViewById(R.id.search);
search1 = (Button) findViewById(R.id.search1);
show = (TextView) findViewById(R.id.show);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//url是要访问的地址 Android是一个单线程系统
//访问线程不能写在UI线程里面
//cmd+ipconfig看自己的ip(192.168.1.31) 手机和电脑必须在同一个网段内
String url = "http://192.168.1.31:8080/HttpTest/index.jsp" +
"?option=getUser&uName=jerehedu";
new MyGetJob().execute(url);
}
});
search1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] arg = new String[2];
arg[0] = "http://192.168.1.31:8080/HttpTest/index.jsp";
arg[1] = "option=getUser&uName=jerehedu";
new MyPostJob().execute(arg);
}
});
}
public class MyPostJob extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection con = null;
//拿数据
InputStream is = null;
StringBuilder sbd = new StringBuilder();
try {
//将url转变为URL类对象
URL url = new URL(params[0]);
//打开URL连接
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5 * 1000);
con.setReadTimeout(5 * 1000);
// 设定请求的方法为"POST",默认是GET
con.setRequestMethod("POST");
// 设置是否从httpUrlConnection读入,默认情况下是true;
con.setDoInput(true);
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
con.setDoOutput(true);
// Post 请求不能使用缓存
con.setUseCaches(false);
//转码 防止乱码
con.setRequestProperty("charset", "UTF-8");
// 窗体数据被编码为 名称/值对 标准编码格式
con.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
//往后台写参数 第一个传进来的是地址
//strings应该是这样的样式=>option=getUser&uName=jerehedu
String strings = params[1];
OutputStream os = con.getOutputStream();
// 向对象输出流写出数据,这些数据将存到内存缓冲区中
os.write(strings.getBytes());
// 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream)
os.flush();
// 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中,
// 在调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器
os.close();
if (con.getResponseCode() == 200) {
is = con.getInputStream();
int next = 0;
byte[] b = new byte[1024];
while ((next = is.read(b)) > 0) {
sbd.append(new String(b, 0, next));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (con != null) {
con.disconnect();
}
}
return sbd.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
show.setText("POST请求结果" + s);
}
}
/**
* AsyncTask异步任务类
* 异步任务类的参数:
* 异步任务类的第一个参数会传到doInBackground方法中
* 第三个参数
* #指定doInBackground方法的返回值
* #doInBackground方法的返回值会被onPostExecute接收
*/
public class MyGetJob extends AsyncTask<String, Void, String> {
//onPreExecute在主线程中执行命令 通常做进度条的初始化
@Override
protected void onPreExecute() {
super.onPreExecute();
}
//doInBackground在子线程中执行命令(耗时操作写在这里)
@Override
protected String doInBackground(String... params) {
//因为要抛异常 所以置为空
// HttpURLConnection用这个可以访问网络 在清单文件中打开访问网络的权限
HttpURLConnection con = null;
//流
InputStream is = null;
StringBuilder sbd = new StringBuilder();
try {
URL url = new URL(params[0]);
con = (HttpURLConnection) url.openConnection();
//设置连接超时时间
con.setConnectTimeout(5 * 1000);
//设置读取超时时间
con.setReadTimeout(5 * 1000);
/**
* http响应码
* 200:成功 404:未找到 500:发生错误
*/
if (con.getResponseCode() == 200) {
// 读取网页内容(字符串流)
is = con.getInputStream();
int next = 0;
byte[] bt = new byte[1024];
while ((next = is.read(bt)) > 0) {
sbd.append(new String(bt, 0, next));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
//关闭
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (con != null) {
//断开连接
con.disconnect();
}
}
return sbd.toString();
}
//执行完doInBackground后返回结果的位置
//String s是结果 对应doInBackground里的第三个参数
//onPostExecute 在UI线程(主线程)中执行命令
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//返回显示结果
show.setText(s);
}
}
}
activity_http 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.dell.jreduch07.HttpActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:textSize="20sp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:text="姓名" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="请输入要查找的姓名"
android:id="@+id/et"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search"
android:text="查询(GET方式)"
android:layout_below="@+id/ll1"
android:layout_alignStart="@+id/search1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search1"
android:text="查询(POST方式)"
android:layout_below="@+id/search"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#b9e7e18e"
android:layout_below="@+id/search1"
android:id="@+id/show"
android:layout_marginLeft="5dp"
android:text="查询结果"/>
</RelativeLayout>
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!