android 学习随笔十(网络:get、post提交数据)

1、get

public class Tools {

    public static String getTextFromStream(InputStream is){
        byte[] b = new byte[1024];
        int len;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            while((len = is.read(b)) != -1){
                bos.write(b, 0, len);
            }
            //把字节数组输出流转换成字节数组,然后用字节数组构造一个字符串
            String text = new String(bos.toByteArray());
            return text;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
        
    }
}

 

public class MainActivity extends Activity {

    
    Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View v){
        
        EditText et_name = (EditText) findViewById(R.id.et_name);
        EditText et_pass = (EditText) findViewById(R.id.et_pass);
        
        final String name = et_name.getText().toString();
        final String pass = et_pass.getText().toString();
        
        Thread t = new Thread(){
            @Override
            public void run() {
                @SuppressWarnings("deprecation")//告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。使用这个注释将警告信息去掉
                String path = "http://169.254.244.136/Web2/servlet/Login?name=" + URLEncoder.encode(name) + //对要提交的表单数据进行url编码
                                                                                        "&pass=" + pass;
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    
                    if(conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        String text = Tools.getTextFromStream(is);
                        
                        Message msg = handler.obtainMessage();
                        msg.obj = text;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        };
        t.start();
    }

}

2、POST 

在浏览器中显示的内容有 HTML、有 XML、有 GIF、还有 Flash ……那么,浏览器是如何区分它们,决定什么内容用什么形式来显示呢?答案是 MIME Type,也就是该资源的媒体类型。

 

public class MainActivity extends Activity {

    
    Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View v){
        
        EditText et_name = (EditText) findViewById(R.id.et_name);
        EditText et_pass = (EditText) findViewById(R.id.et_pass);
        
        final String name = et_name.getText().toString();
        final String pass = et_pass.getText().toString();
        
        Thread t = new Thread(){
            @Override
            public void run() {
                String path = "http://169.254.244.136/Web2/servlet/Login";
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    
                    conn.setRequestMethod("POST");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    
                    //添加post请求头中自动添加的属性
                    //流里的数据的mimetype,它的作用是告诉可以处理的文件的类型,一定要编码,否则会出现乱码
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    String content = "name=" + URLEncoder.encode(name) + "&pass=" + pass;
                    //流里数据的长度
                    conn.setRequestProperty("Content-Length", content.length() + "");
                    
                    //打开连接对象的输出流
                    conn.setDoOutput(true);
                    //获取连接对象的输出流
                    OutputStream os = conn.getOutputStream();
                    //把数据写入输出流中,准备把输出流发送给服务器
                    os.write(content.getBytes());
                    
                    if(conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        String text = Tools.getTextFromStream(is);
                        
                        Message msg = handler.obtainMessage();
                        msg.obj = text;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        };
        t.start();
    }

}

 

posted @ 2016-09-22 10:38  ecollab  阅读(163)  评论(0编辑  收藏  举报