zoukankan      html  css  js  c++  java
  • JDK服务器存储

    public class fuwuqi extends AppCompatActivity {
    
        EditText et;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fuwuqi);
            et=(EditText)findViewById(R.id.et1);
    }  //用来显示结果
        String s="";
        public void b1(View v){
            //启动进度对话框
            final ProgressDialog pg=ProgressDialog.show(this,null,"请稍后");
            s="";
            //启动子线程访问远程服务器
            new  Thread(){
                @Override
                public void run() {
                    //访问远程服务器
                    //jdk  get
                    HttpURLConnection huc=null;
                    try {
    
    
                        //1.构造url对象
                        URL url = new URL("http://192.168.0.101:81/index.asp?name=tom&password=123");
    
                        //2.得到连接 HttpURLConnection
                         huc=(HttpURLConnection)url.openConnection();
    
                        //3.设置HttpURLConnection
                        //连接方式
                        huc.setRequestMethod("GET");
                        //超时处理
                        huc.setConnectTimeout(3000);
                        huc.setReadTimeout(3000);
    
                        //4.连接远程服务器
                        huc.connect();
    
                        //5.接受响应报文的状态码
                       int code= huc.getResponseCode();
    
                        //6.判断响应状态码是否=200
                        if(code==200){
                            //1.接收数据
    
                            //2.得到数据流
                            InputStream is = huc.getInputStream();
    
                            byte[] bytes = new byte[1024];
    
                            //读到的报文长度
                            int i=0;
                            while ((i=is.read(bytes))>0){
                                //接收字符串
                                s+= new String(bytes,0,i);
                            }
                            //关闭流
                            is.close();
                        }
                        else {
                            s="响应错误,错误码="+code;
                        }
    
    
                        //7.显示结果,不能直接访问主线程的视图,runOnUiThread
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                et.setText(s);
    
                            }
                        });
    
    
    
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    finally {
                        //8.关闭连接和进度对话框
                        // 释放
                        if(huc!=null){
                        huc.disconnect();}
                        //关闭对话框,支持跨线程
                        pg.dismiss();
                    }
    
                }
            }.start();
    
    
        }
    
        public void b2(View v){
            //启动进度对话框
            final ProgressDialog pg=ProgressDialog.show(this,null,"请稍后");
            s="";
            //启动子线程访问远程服务器
            new  Thread(){
                @Override
                public void run() {
                    //访问远程服务器
                    //jdk  get
                    HttpURLConnection huc=null;
                    try {
    
    
                        //1.构造url对象
                        URL url = new URL("http://192.168.0.101:81/index.asp");
    
                        //2.得到连接 HttpURLConnection
                        huc=(HttpURLConnection)url.openConnection();
    
                        //3.设置HttpURLConnection
                        //连接方式
                        huc.setRequestMethod("POST");
                        //超时处理
                        huc.setConnectTimeout(3000);
                        huc.setReadTimeout(3000);
    
    
                        //4.连接远程服务器
                        huc.connect();
    
                                 //数据放到请求体里
                        //1)得到输出流
                        OutputStream os=huc.getOutputStream();
                        String out="name=tom2&password=1234";
                        os.write(out.getBytes("UTF-8"));
                        //5.接受响应报文的状态码
                        int code= huc.getResponseCode();
    
                        //6.判断响应状态码是否=200
                        if(code==200){
                            //1.接收数据
    
                            //2.得到数据流
                            InputStream is = huc.getInputStream();
    
                            byte[] bytes = new byte[1024];
    
                            //读到的报文长度
                            int i=0;
                            while ((i=is.read(bytes))>0){
                                //接收字符串
                                s+= new String(bytes,0,i);
                            }
                            //关闭流
                            is.close();
                            os.close();
                        }
                        else {
                            s="响应错误,错误码="+code;
                        }
    
    
                        //7.显示结果,不能直接访问主线程的视图,runOnUiThread
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                et.setText(s);
    
                            }
                        });
    
    
    
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    finally {
                        //8.关闭连接和进度对话框
                        // 释放
                        if(huc!=null){
                            huc.disconnect();}
                        //关闭对话框,支持跨线程
                        pg.dismiss();
                    }
    
                }
            }.start();
    
    
        }
  • 相关阅读:
    rest_framework学习之路
    jQuery操作cookie
    Cookie和Session
    HTTP之Content-Type
    HTTP协议
    Python之random模块
    HTML5(FileRdeader)
    Python之re模块
    LINQ基础 之 LINQ TO SQL (二)
    LINQ基础(一)
  • 原文地址:https://www.cnblogs.com/storm47/p/5584066.html
Copyright © 2011-2022 走看看