您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection的GET请求POST请求小总结

2016-09-24 18:33 267 查看
先搭建一个简单的服务器,具体方法见我另外一篇文章测试HttpUrlConnection请求时如何搭建一个简单的服务器

GET请求代码如下:

//网络请求是一个耗时操作,要在子线程里面开启
new Thread(new Runnable() {
@Override
public void run() {
try {
//get请求的url
URL url=new URL("http://192.168.2.135:8080/web/MyProject?username=zhangsan&password=123");
//url写法: 协议://IP地址:端口号/访问的文件
//协议一般是http,IP地址是要访问的IP地址,端口一般是8080,然后加上访问的是什么
//新建一个HttpURLConnection,从url开启一个连接
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//设置请求的模式
connection.setRequestMethod("GET");
//设置请求连接超时时间
connection.setConnectTimeout(5000);
//设置访问时的超时时间
connection.setReadTimeout(5000);
//开启连接
connection.connect();
InputStream inputStream=null;
BufferedReader reader=null;
//如果应答码为200的时候,表示成功的请求带了,这里的HttpURLConnection.HTTP_OK就是200
if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
//获得连接的输入流
inputStream=connection.getInputStream();
//转换成一个加强型的buffered流
reader=new BufferedReader(new InputStreamReader(inputStream));
//把读到的内容赋值给result
final String result=reader.readLine();
//子线程不能更新UI线程的内容,要更新需要开启一个Ui线程,这里Toast要在Ui线程
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
});
}
//关闭流和连接
reader.close();
inputStream.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}).start();POST请求是看不到URL里面的头文件内容的,把头文件内容从连接的输出流中写入,这样相对于GET请求会比较安全些
//网络请求是一个耗时操作,要在子线程里面开启
new Thread(new Runnable() {
@Override
public void run() {
try {
//get请求的url
URL url=new URL("http://192.168.2.135:8080/web/MyProject");
//url写法: 协议://IP地址:端口号/访问的文件
//协议一般是http,IP地址是要访问的IP地址,端口一般是8080,然后加上访问的是什么
//新建一个HttpURLConnection,从url开启一个连接
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//设置请求的模式
connection.setRequestMethod("POST");
//设置请求连接超时时间
connection.setConnectTimeout(5000);
//设置访问时的超时时间
connection.setReadTimeout(5000);
//设置连接允许写入
connection.setDoOutput(true);
//设置头文件内容
String data="username=zhangsan&password=123";
//获得连接的输出流
OutputStream outputStream = connection.getOutputStream();
//把头文件内容写入流中
outputStream.write(data.getBytes());
//开启连接
connection.connect();
InputStream inputStream=null;
BufferedReader reader=null;
//如果应答码为200的时候,表示成功的请求带了,这里的HttpURLConnection.HTTP_OK就是200
if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
//获得连接的输入流
inputStream=connection.getInputStream();
//转换成一个加强型的buffered流
reader=new BufferedReader(new InputStreamReader(inputStream));
//把读到的内容赋值给result
final String result=reader.readLine();
//子线程不能更新UI线程的内容,要更新需要开启一个Ui线程,这里Toast要在Ui线程
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
});
}
//关闭流和连接
reader.close();
inputStream.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}).start();简单的下载服务器图片的代码
new Thread(new Runnable() {
@Override
public void run() {
try {
//创建一个url,指定要下载的图片的url
URL url=new URL("http://192.168.2.135:8080/web/aaa.bmp");
//从url打开连接,下载图片默认是GET请求
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//连接
connection.connect();
//如果应答码是200,代表连接上了
if(connection.getResponseCode()==200){
//从连接获得输入流
InputStream inputStream=connection.getInputStream();
//把输入流转成bitmap
final Bitmap map= BitmapFactory.decodeStream(inputStream);
runOnUiThread(new Runnable() {
@Override
public void run() {
//把bitmap设置到控件上
imageView.setImageBitmap(map);
}
});
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();简单的下载某个文件的代码
//设置一个progressdialog
final ProgressDialog diglog=new ProgressDialog(this);
//设置diglog的样式
diglog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置diglog显示出来
diglog.show();
new Thread(new Runnable() {
@Override
public void run() {
try {
//要下载文件的url
URL url=new URL("http://192.168.2.135:8080/web/mymusic.mp3");
//从url打开连接
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//获得文件的大小
diglog.setMax(connection.getContentLength());
//建立连接
connection.connect();
//设置下载到的地方
final File file=new File(Environment.getExternalStorageDirectory(),"/mymusic.mp3");
if(connection.getResponseCode()==200){
//获得连接的输入流
InputStream inputStream=connection.getInputStream();
//新建一个输出流
FileOutputStream fos=new FileOutputStream(file);
//设置每次读取的内容
byte[] bytes=new byte[1024];
//设置每次读取到的内容的长度
int len=-1;
while ((len=inputStream.read(bytes))!=-1){
//把读到的内容写入到内存里
fos.write(bytes,0,len);
//设置diglog每次的增长值
diglog.incrementProgressBy(len);
}
//文件下载完设置进度条隐藏
diglog.dismiss();
//关闭流和连接
fos.close();
inputStream.close();
}
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

最后要提下注意的几点
子线程是一个耗时操作,不能在子线程中更新UI

如果要在子线程中更新UI,可以开启一个UI线程,或者使用消息机制

还要在清单配置文件那边加上INTERNET权限

ProgressDiglog是个特殊的控件,可以在子线程中来dissmiss掉

下载文件的时候,注意下载了什么格式的文件,下载下来要存到SD卡里的文件就要是什么格式

如果要下载很多张图片,用Bitmap会有OutofMemory(内存溢出)风险,

这里可以使用第三方来解决,比如Blide,Imageloader
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网络 android 线程 url