android網絡編程之HttpUrlConnection的講解--GET請求


1、服務器后台使用Servlet開發,這里不再介紹。

2、測試機通過局域網鏈接到服務器上,可以參考我的博客:http://www.cnblogs.com/begin1949/p/4905192.html。(當初以為可以直接通過USB訪問http://127.0.0.1:8080/)。

3、網絡開發不要忘記在配置文件中添加訪問網絡的權限

<uses-permission android:name="android.permission.INTERNET"/>

4、網絡請求、處理不能在主線程中進行,一定要在子線程中進行。因為網絡請求一般有1~3秒左右的延時,在主線程中進行造成主線程的停頓,對用戶體驗來說是致命的。(主線程應該只進行UI繪制,像網絡請求、資源下載、各種耗時操作都應該放到子線程中)。

5、

public class GetActivity extends Activity {
    private TextView mTvMsg;
    
    private String result;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get);
        
        initView();
    }
    
    private void initView(){
        mTvMsg = (TextView) findViewById(R.id.tv_servlet_msg);
        
        new Thread(getThread).start();
    }
    
    private Thread getThread = new Thread(){
        public void run() {
            HttpURLConnection connection = null;
            try {
                URL url = new URL("http://192.168.23.1:8080/TestProject/GetTest");
                connection = (HttpURLConnection) url.openConnection();
                // 設置請求方法,默認是GET
                connection.setRequestMethod("GET");
                // 設置字符集
                connection.setRequestProperty("Charset", "UTF-8");
                // 設置文件類型
                connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
                // 設置請求參數,可通過Servlet的getHeader()獲取
                connection.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8"));
                // 設置自定義參數
                connection.setRequestProperty("MyProperty", "this is me!");
                
                if(connection.getResponseCode() == 200){
                    InputStream is = connection.getInputStream();
                    result = StringStreamUtil.inputStreamToString(is);
                    
                    Message msg = Message.obtain();
                    msg.what = 0;
                    getHandler.sendMessage(msg);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if(connection != null){
                    connection.disconnect();
                }
            }
        };
    };
    
    private Handler getHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            if(msg.what == 0 && result!=null){
                mTvMsg.setText(result);
            }
        };
    };
}

6、

7、請求參數可以通過URLEncoder.encode("你好", "UTF-8")進行編碼,URLDecoder.decode("", "UTF-8")進行解碼。這里URLEncoder會對等號"="進行編碼,這里要注意一下。

8、這里可以通過connection.setRequestProperty("MyProperty", "this is me!")進行參數傳遞,通過Servlet的getHeader()獲得該參數。我想它的安全性應該比直接拼接到URL上面安全。

9、第一步:實例化URL對象。

    第二步:實例化HttpUrlConnection對象。

    第三步:設置請求連接屬性,傳遞參數等。

    第四步:獲取返回碼判斷是否鏈接成功。

    第五步:讀取輸入流。

    第六步:關閉鏈接。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM