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对象。

    第三步:设置请求连接属性,传递参数等。

    第四步:获取返回码判断是否链接成功。

    第五步:读取输入流。

    第六步:关闭链接。

posted on 2015-12-19 15:42  StephenHe  阅读(16168)  评论(0编辑  收藏  举报

导航