Android HTTP 数据提交
Posted ぃ流年☆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"/>