码迷,mamicode.com
首页 > 移动开发 > 详细

android网络编程之HttpUrlConnection的讲解--GET请求

时间:2015-12-19 16:22:34      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

1、服务器后台使用Servlet开发,这里不再介绍。

2、网络开发不要忘记在配置文件中添加访问网络的权限

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

3、网络请求、处理不能在主线程中进行,一定要在子线程中进行。因为网络请求一般有1~3秒左右的延时,在主线程中进行造成主线程的停顿,对用户体验来说是致命的。(主线程应该只进行UI绘制,像网络请求、资源下载、各种耗时操作都应该放到子线程中)。

4、

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);
            }
        };
    };
}

5、

技术分享

6、请求参数可以通过URLEncoder.encode("你好", "UTF-8")进行编码,URLDecoder.decode("", "UTF-8")进行解码。这里URLEncoder会对等号"="进行编码,因此只能对参数进行多次编码。

7、这里可以通过connection.setRequestProperty("MyProperty", "this is me!")进行参数传递,通过Servlet的getHeader()获得该参数。我想它的安全性应该比直接拼接到URL上面安全。

android网络编程之HttpUrlConnection的讲解--GET请求

标签:

原文地址:http://www.cnblogs.com/begin1949/p/4905282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!