httpURLConnection post

    // 开始转圈
        final ProgressDialog dialog = ProgressDialog.show(this,null, "加载中");
        // 在分线程中请求数据
        new Thread(){
            public void run(){
                // 获取URL
                URL url;
                try {
                    url = new URL("http://pkuie.euming.com/urming_pkuie/system/getMyIndex?offset=0&pageSize=10&accessToken=WZ2B_Z8dCw2TveUu6WOM6cCfvbgLvcYG6A6fywmL6cYpvbY&_=1477736373144");
                    // 得到URLConnection
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    // 设置请求的方式 响应时长 读取时长
                    connection.setConnectTimeout(2000);
                    connection.setReadTimeout(2000);
                    connection.setRequestMethod("POST");
                    // 开启连接
                    connection.connect();
                    // 设置请求的参数
                    OutputStream outputStream2 = connection.getOutputStream();
                    String paramsString = "?offset=0&pageSize=10&accessToken=WZ2B_Z8dCw2TveUu6WOM6cCfvbgLvcYG6A6fywmL6cYpvbY&_=1477736373144";
                    outputStream2.write(paramsString.getBytes());
                    // 得到数据的输入流
                    InputStream inputStream = connection.getInputStream();
                    // 将流进行写入操作
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while((len=inputStream.read(buffer))!=-1){
                        outputStream.write(buffer, 0, len);
                    }
                    // 将数据转化成String
                    final String string = outputStream.toString();
                    // 关闭流
                    outputStream.close();
                    inputStream.close();
                    // 关闭连接
                    connection.disconnect();
                    // 在主线程中关闭dialog 显示数据
                    runOnUiThread(new Runnable() {
                        public void run() {
                            dialog.dismiss();
                            Log.e("sb", string);
                        }
                    });
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();

你可能感兴趣的:(httpURLConnection post)