Android发送Post请求

08 082016
//Android发送Post请求
Thread thread = new Thread(new Runnable() {
    
    //请求服务器地址
    String server = "http://192.168.1.102/index.php/index/member/register";
    @Override
    public void run() {
    
        try {
                //new url
            URL url = new URL(server);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                                   
            conn.setRequestMethod("POST");  //Post请求
            conn.setDoInput(true);   
            conn.setDoOutput(true);

            String param = "username=15612341153&pwd1=123&pwd2=123";
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(param);

            InputStream in = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));

            String s1;
            String s2 = "";
            while((s1=bf.readLine()) != null){
                s2 = s2 + s1;
            }

            //Toast something

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
});

thread.start();


版权信息

原文作者: 石头记

原文链接: http://1m85.com/9.html

版权所有: 本文为博主原创文章,转载请标明出处。