Android入門のHttpリクエスト方式GetとPost

3932 ワード

前言:Android開発の過程で、データインタラクション(データへのアクセス、データの書き込みなど)に触れなければならない.データのインタラクションに触れた以上、自然と通信間のプロトコルを使って要求を行う.最も一般的なプロトコルはHttpプロトコルであり、Httpプロトコルには2つの具体的な要求方式-netとPostが含まれている.
  • Http要求方式GetとPostの概要Httpプロトコル:Http(HyperText Transfer Protocolハイパーテキスト転送プロトコル)は、クライアントとサーバが円滑に通信できるように設計されたプロトコルである.HTTPはクライアントとサーバの間でrequest-response protocol(リクエスト-返信プロトコル)で動作する.簡単に言えば、GetとPostはhttpプロトコルに基づくネットワークデータインタラクション方式です.
  • GetとPostの主な違いAndroid開発の過程で、HttpのGetとPostをどのように選んで通信すればいいのでしょうか.では、彼らの違いを詳しく探ってみましょう.1.getは通常サーバからデータを取得し、postは通常サーバにデータを転送する.2.getとは、フォームのACTION属性が指すURLにパラメータデータキューを加えたもので、値とフォーム内の各フィールドが1つ1つ対応しており、URLで見ることができますが、実際にはURL接続方式です.postは、HTTPpostメカニズムにより、フォーム内の各フィールドをその内容とともにHTML HEADER内に配置してACTION属性が指すURLアドレスに転送する.3.get方式ではサーバ側がRequest.QueryStringで変数の値を取得し、post方式ではサーバ側がRequest.Formでコミットしたデータを取得する.4.get転送のデータ量は小さく、1 KBを超えることはできない[IE,Oher:4].post転送のデータ量は大きく、一般的には制限されないようにデフォルトされています.しかし、理論的には、IIS 4の中で最も多くは80 KBであり、IIS 5の中では100 KBである.5.getセキュリティは非常に低く、postセキュリティは高い.
  • AndroidがGetとPostプロトコルをどのように使用するかは言うまでもなく、上コード展示(ユーザーログインアクセスサーバのプレゼンテーション)
  • public class LoginServer {    
       /**     
       *get          
       *@param username         
       *@param password        
       *@return   null          
       */ 
    public static String loginByGet(String username,String password){
            //get       url     
            String path = "http://172.16.168.111:1010/login.php?username="+username+"&password="+password;
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                //     
                int responseCode = connection.getResponseCode();
                if(responseCode ==200){
                    //           
                    InputStream is = connection.getInputStream();
                    return IOSUtil.inputStream2String(is);
                }else {
                    //    
                    return null;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
       /** * post      
       *@param username     
       *@param password    
       *@return   null      
       */
        public static String loginByPost(String username,String password){
            String path = "http://172.16.168.111:1010/login.php";
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("POST");
    
                //    
                String data = "username="+username+"&password="+password;
                //           
                connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Length", data.length()+"");
    
                //post                   
                connection.setDoOutput(true);
                OutputStream outputStream = connection.getOutputStream();
                outputStream.write(data.getBytes());
    
                //     
                int responseCode = connection.getResponseCode();
                if(responseCode ==200){
                    //    
                    InputStream is = connection.getInputStream();
                    return IOSUtil.inputStream2String(is);
                }else {
                    //    
                    return null;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }