httpurlconnection postdemo

 new Thread(){
            
            public void run() {
                
                try {
                    URL url = new URL(path);
                    HttpURLConnection http = (HttpURLConnection) url.openConnection();
                    http.setReadTimeout(timeout);
                    http.setRequestMethod("POST");
                    http.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
//请求信息 
                    String data = "offset=0&pageSize=20&accessToken=";
                    http.setRequestProperty("Content-Length", data.length()+"");
                    http.setDoOutput(true);
//请求信息params
                    http.getOutputStream().write(data.getBytes());
                    int responseCode = http.getResponseCode();
                    if (responseCode==200){
                        InputStream inputStream = http.getInputStream();
                        
                        ByteArrayOutputStream os = new ByteArrayOutputStream();
                        int len = 0;
                        byte[] buffer = new byte[1024];
                        while((len=inputStream.read(buffer))>0) {
                            os.write(buffer, 0, len);
                        }
                        String string = os.toString();
                        Log.e("haha", string);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("haha", "---------------");
                }
                
            };
            
        }.start();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容