zoukankan      html  css  js  c++  java
  • 食堂APP-项目开发及踩坑记录(7)

    实现APP中远程访问调用servlet并返回数据,

     private void send() {
            //开启线程,发送请求
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpURLConnection connection = null;
                    BufferedReader reader = null;
                    try {
                        EditText editText =(EditText)findViewById(R.id.editText);
                        EditText editText1 =(EditText)findViewById(R.id.editText1);
                        String timeend = editText.getText().toString();
                        String sheng=editText1.getText().toString();
                        URL url = new URL("http://192.168.1.6:8080/APP/Servlet?timeend="+timeend+"&sheng="+sheng);
    
                        //URL url = new URL("https://www.baidu.com/");
                        connection = (HttpURLConnection) url.openConnection();
                        //设置请求方法
                        connection.setRequestMethod("GET");
                        //设置连接超时时间(毫秒)
                        connection.setConnectTimeout(5000);
                        //设置读取超时时间(毫秒)
                        connection.setReadTimeout(5000);
    
                        //返回输入流
                        InputStream in = connection.getInputStream();
    
                        //读取输入流
                        reader = new BufferedReader(new InputStreamReader(in));
                        StringBuilder result = new StringBuilder();
                        String line;
                        while ((line = reader.readLine()) != null) {
                            result.append(line);
                        }
                        show(result.toString());
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (reader != null) {
                            try {
                                reader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (connection != null) {//关闭连接
                            connection.disconnect();
                        }
                    }
                }
            }).start();
        }
    

      

  • 相关阅读:
    VIJOS P1057盖房子 (动态规划)
    RQNOJ PID57 / 找啊找啊找GF
    RQNOJ PID302 / [NOIP2001]统计单词个数 (动态规划)
    hdu 3829 Cat VS Dog 最大独立集
    并查集 找k颗树使节点数最多
    在 Sublime Text 3 中配置编译和运行 Java 程序
    在 Sublime Text 3 中配置编译和运行 Java 程序
    StarUML license key
    StarUML license key
    测试对于list的sort与sorted的效率
  • 原文地址:https://www.cnblogs.com/hunfen/p/12793716.html
Copyright © 2011-2022 走看看