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

Android HTTP 数据提交

时间:2017-07-06 20:44:52      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:exce   man   ==   pen   nbsp   roi   ace   logs   cti   

 

在Android 项目中,使用HTTP协议获取数据或者处理数据,需要使用到多线程和配置相应的APP权限

1、使用线程,使用HTTP 提交数据

 private Thread submitThread = new Thread() {
        public void run() {
            HttpURLConnection connection = null;
            try {
                URL url = new URL(serviceURL+"SaveTestResult?bc="+bc+");
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Charset", "UTF-8");
                connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
                if (connection.getResponseCode() == 200) {
                    InputStream is = connection.getInputStream();
                    result = ServiceHelper.convertStreamToString(is);
                    Message msg = Message.obtain();
                    msg.what = 0;
                    subitHandler.sendMessage(msg);
                }
                
} catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }; private Handler subitHandler = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 0 ) { Toast.makeText(ViewTest.this, "提交成功,下一个", Toast.LENGTH_SHORT).show(); } } };

2、配置项目权限

在AndroidManifest.xml 添加以下代码

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

 

Android HTTP 数据提交

标签:exce   man   ==   pen   nbsp   roi   ace   logs   cti   

原文地址:http://www.cnblogs.com/jerrywublogs/p/7127750.html

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