发送请求获取远程数据

        new Thread() {
            public void run() {
                // 发送请求获取数据,参数则为请求json的链接地址
                try {
                    URL url = new URL("http://192.168.13.9:8080/update.json");
                    //开启链接
                    HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
                    openConnection.setConnectTimeout(2000);
                    openConnection.setReadTimeout(2000);
                    openConnection.setRequestMethod("GET");
                    //获取响应码
                    if(openConnection.getResponseCode()==200){
                        InputStream is = openConnection.getInputStream();
                        String json = StreamUtil.stream2String(is);
                        //json解析
                    }
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();

猜你喜欢

转载自blog.csdn.net/fuyinghaha/article/details/82259821
今日推荐