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();
    
    
        }
  • 相关阅读:
    微信 token ticket jsapi_ticket access_token 获取 getAccessToken get_jsapi_ticket方法
    PHP 日志 记录 函数 支持 数组 对象 新浪 sae 环境 去掉 空格 换行 格式化 输出 数组转字符串
    原生 原始 PHP连接MySQL 代码 参考mysqli pdo
    PHP 数字金额转换成中文大写金额的函数 数字转中文
    使用PHPMailer发送带附件并支持HTML内容的邮件
    设置输出编码格式 header 重定向 执行时间 set_time_limit 错误 报告 级别 error_reporting
    html5 bootstrap pannel table 协议 公告 声明 文书 模板
    指向指针的指针
    二级指针
    c语言:当指针成为参数后
  • 原文地址:https://www.cnblogs.com/storm47/p/5584066.html
Copyright © 2011-2022 走看看