• 采用Post方式提交数据实例


    项目目录

    一、编写MainActivity.java

    package com.hyzhou.getdemo;
    
    import com.hyzhou.getdemo.service.LoginServer;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private EditText et_username, et_password;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et_username = (EditText) findViewById(R.id.et_username);
            et_password = (EditText) findViewById(R.id.et_password);
        }
    
        public void click(View view) {
            final String username = et_username.getText().toString().trim();
            final String password = et_password.getText().toString().trim();
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    final String result = LoginServer
                            .loginByPost(username, password);
                    if (result != null) {
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, result, 0).show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, "请求失败", 0).show();
                            }
                        });
                    }
                }
            }).start();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    View Code

    二、编写LoginServer.java

    /**
     * 
     */
    package com.hyzhou.getdemo.service;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import com.hyzhou.getdemo.utiils.StreamTools;
    
    /**
     * @author hyzhou
     *
     * 2013-11-6
     */
    public class LoginServer {
        public static String loginByPost(String username,String password)
        {
            
            try {
                String path="http://192.168.1.54:8080/web/LoginServlet";
                URL url=new URL(path);
                HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("POST");
                //准备数据
                String data="username="+username+"&password="+password;
                conn.setRequestProperty("content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Content-Length", data.length()+"");
                //post 的方式实际上是浏览器把数据写给服务器
                conn.setDoOutput(true);
                OutputStream os=conn.getOutputStream();
                os.write(data.getBytes());
                
                int code=conn.getResponseCode();
                if (code==200) {
                    InputStream is=conn.getInputStream();
                    String text=StreamTools.readInputStream(is);
                    return text;
                }else {
                    return null;
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }
    View Code

    三、编写StreamTools.java

    /**
     * 
     */
    package com.hyzhou.getdemo.utiils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    
    /**
     * @author hyzhou
     *
     * 2013-11-6
     */
    public class StreamTools {
    
        /**
         * 把输入流内容转化成字符串
         */
        public static String readInputStream(InputStream is) {        
            try {
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                int len=0;
                byte[] buffer=new byte[1024];
                while ((len=is.read(buffer))!=-1) {
                    baos.write(buffer,0,len);
                /**
     * 
     */
    package com.hyzhou.getdemo.utiils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    
    /**
     * @author hyzhou
     *
     * 2013-11-6
     */
    public class StreamTools {
    
        /**
         * 把输入流内容转化成字符串
         */
        public static String readInputStream(InputStream is) {        
            try {
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                int len=0;
                byte[] buffer=new byte[1024];
                while ((len=is.read(buffer))!=-1) {
                    baos.write(buffer,0,len);
                }
                is.close();
                baos.close();
                byte[] result=baos.toByteArray();
                //试着解析result中的字符串
                String temp=new String(result);
                return temp;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                return "获取失败";
            }
            
        }
    }
    /**
     * 
     */
    package com.hyzhou.getdemo.utiils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    
    /**
     * @author hyzhou
     *
     * 2013-11-6
     */
    public class StreamTools {
    
        /**
         * 把输入流内容转化成字符串
         */
        public static String readInputStream(InputStream is) {        
            try {
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                int len=0;
                byte[] buffer=new byte[1024];
                while ((len=is.read(buffer))!=-1) {
                    baos.write(buffer,0,len);
                }
                is.close();
                baos.close();
                byte[] result=baos.toByteArray();
                //试着解析result中的字符串
                String temp=new String(result);
                return temp;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                return "获取失败";
            }
            
        }
    }
    View Code

     PS:相对get请求,Post请求相对复杂,需要指定Content-Type、Content-Length等

  • 相关阅读:
    FFmpeg命令:几种常见场景下的FFmpeg命令(摄像头采集推流,桌面屏幕录制推流、转流,拉流等等)
    javaCV入门指南:序章
    javacpp-FFmpeg系列补充:FFmpeg拉流截图实现在线演示demo(视频截图并返回base64图像,支持jpg/png/gif/bmp等多种格式)
    javacpp-FFmpeg系列之3: 像素图像数据转换(BGR与BufferdImage互转,RGB与BufferdImage互转,BufferdImage转Base64编码)
    javacpp-FFmpeg系列补充:FFmpeg解决avformat_find_stream_info检索时间过长问题
    javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转换为YUV、BGR24或RGB24等图像像素数据
    Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)
    运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】
    javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片
    spring-data详解之spring-data-jpa:简单三步快速上手spring-data-jpa开发
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3410932.html
Copyright © 2020-2023  润新知