首页 > 代码库 > Android HTTP 数据提交

Android HTTP 数据提交

 

在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 数据提交