使用HttpConnection

1.URL:包含请求地址的类

url(path):包含请求路径的构造方法

openConnection():得到连接对象

2.HttpURLConnection:代表与服务器连接的类

setMethod("GET/POST"):设置请求方式

setConnectionTimeout(time):设置连接超时时间,单位为ms

setReadTimeout(time):设置读取服务器返回数据的时间

connect():连接服务器

int getResponseCode():得到服务器返回的结果吗

int getContentLength():得到服务器返回数据的长度(字节)

getOutputStream():返回一个指向服务器端的数据输出流

getInputStream():返回一个从服务器端返回的数据输入流

disconnect():断开连接

get请求

//显示ProgressDialog                final ProgressDialog dialog = ProgressDialog.show(netActivity.this,null,"正在请求中...");                //启动分线程                new Thread() {                    @Override                    public void run() {                        //在分线程,发送请求,得到响应数据                        try {                            //得到path                            String path = et_net_url.getText().toString()+"?name=Tom&age=12";                            //创建URL对象                            URL url = new URL(path);                            //打开连接,得到HttpURLConnection对象                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();//                            设置请求方式,连接超时,读取数据超时                            connection.setRequestMethod("GET");                            connection.setConnectTimeout(5000);                            connection.setReadTimeout(6000);                            //链接服务器                            connection.connect();                            //发送请求,得到响应数据                            int responseCode = connection.getResponseCode();                            //必须是200才读                            if (responseCode==200) {                                //得到InputStream,读取成string                                InputStream is = connection.getInputStream();                                ByteArrayOutputStream baos = new ByteArrayOutputStream();                                byte [] buffer = new byte[2014];                                int len =-1;                                while ((len=is.read(buffer))!=-1) {                                    baos.write(buffer,0,len);                                }                                final String result = baos.toString();                                baos.close();                                is.close();                                runOnUiThread(new Runnable() {                                    @Override                                    public void run() {                                        et_net_show.setText(result);                                        dialog.dismiss();                                    }                                });                            }                            connection.disconnect();                        } catch (Exception e) {                            e.printStackTrace();                            dialog.dismiss();                        }                    }                }.start();
post请求

 //显示ProgressDialog                final ProgressDialog dialog = ProgressDialog.show(netActivity.this, null, "正在加载中....");                //启动分线程                new Thread(){                    public void run() {                        try {                            String path = et_net_url.getText().toString(); //得到path                            URL url = new URL(path);//创建URL对象                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();//打开链接,得到HttpURLConnection连接对象                            connection.setRequestMethod("POST");//设置请求方式                            connection.setConnectTimeout(5000);//设置连接时间                            connection.setReadTimeout(5000);//设置读取时间                            connection.connect();//打开链接                            //发请求,得到响应数据                            //得到输出流,写请求体:name=Tom&age=21                            OutputStream os = connection.getOutputStream();                            String data = "name=哈哈&age=21";                            os.write(data.getBytes("utf-8"));                            int responseCode = connection.getResponseCode();                            if (responseCode==200) {                                InputStream is = connection.getInputStream();                                ByteArrayOutputStream baos = new ByteArrayOutputStream();                                byte[] buffer = new byte[2014];                                int len=-1;                                while ((len=is.read(buffer))!=-1) {                                    baos.write(buffer,0,len);                                }                                final String result = baos.toString();                                baos.close();                                is.close();                                runOnUiThread(new Runnable() {                                    @Override                                    public void run() {                                        et_net_show.setText(result);                                        dialog.dismiss();                                    }                                });                                connection.disconnect();                            }                            os.close();                        } catch (Exception e) {                            e.printStackTrace();                            dialog.dismiss();                        }                    }                } .start();



更多相关文章

  1. android系统之sensor学习
  2. Android(安卓)Mqtt重连的一个小问题
  3. Android(安卓)Studio连接不上模拟器的解决方法
  4. 利用Android自带的http包进行网络请求
  5. Android(安卓)几种网络请求。
  6. Android(安卓)Retrofit网络请求Service,@Path、@Query、@QueryMap
  7. Android分页中显示出下面翻页的导航栏的布局实例代码
  8. Android如何通过content provider构建媒体文件数据库
  9. Android之Retrofit2.0 处理返回json报文并转换成bean对象

随机推荐

  1. Android学习笔记:Handler
  2. android servicemanager与binder源码分析
  3. Cocos2d-x shareSDK
  4. Android(安卓)使用【AIDL】调用外部服务
  5. 用C/C++开发android应用
  6. Android(安卓)串口开发知识总结(未完待续)
  7. android应用开发入门
  8. windowSoftInputMode属性详解(Android)
  9. android 安卓系统
  10. Android多线程系统概述(sundy深入浅出)之进