Android中基于HTTP协议的网络访问的两种方式
2016-05-12 18:10
791 查看
Android中基于HTTP协议的网络访问的两种方式:
1)HttpClient (apche开发)
2)HttpURLConnection (google在发布安卓时在Java基础上修改得到的)
使用HC(HttpClient)/UC(HttpURLConnection)进行网络访问的基本步骤:
1.申请权限(INTERNET访问权限)
2.任何网络访问的相关代码,必须在工作线程中执行
3.创建HC/UC对象
4.声明发起网络访问的方式(GET/POST)
5.进行网络连接
6.获得服务器响应的结果
7.解析结果,提取需要的内容
8.解析结果要提交到UI线程进行呈现
使用HttpClient进行网络访问
使用HttpURLConnection进行网络访问
1)HttpClient (apche开发)
2)HttpURLConnection (google在发布安卓时在Java基础上修改得到的)
使用HC(HttpClient)/UC(HttpURLConnection)进行网络访问的基本步骤:
1.申请权限(INTERNET访问权限)
2.任何网络访问的相关代码,必须在工作线程中执行
3.创建HC/UC对象
4.声明发起网络访问的方式(GET/POST)
5.进行网络连接
6.获得服务器响应的结果
7.解析结果,提取需要的内容
8.解析结果要提交到UI线程进行呈现
使用HttpClient进行网络访问
public static void registUser(Context context, final User user, final OnRegistFinishListener listener) { new AsyncTask<Void, Void,String>(){ @Override protected String doInBackground(Void... params) { try { HttpClient client=new DefaultHttpClient(); HttpPost post=new HttpPost("http://172.88.134.139:8080/ems/regist.do"); //添加一个请求头,对请求实体中的参数做一个说明 post.setHeader("Content-Type","application/x-www-from-urlencoded"); //在post中添加请求参数 //请求参数会添加在请求实体中 List<NameValuePair> parameters=new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("loginname", user.getName())); parameters.add(new BasicNameValuePair("password", user.getPassword())); parameters.add(new BasicNameValuePair("relaname", user.getRealname())); parameters.add(new BasicNameValuePair("email", user.getEmail())); //将数据放到实体中 HttpEntity entity=new UrlEncodedFormEntity(parameters); //提交数据 post.setEntity(entity); //服务器返回响应 HttpResponse resp=client.execute(post); //将响应转化为实体 HttpEntity respEntity=resp.getEntity(); //将实体读出来(读一行) String line=EntityUtils.toString(respEntity); return line; } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(String result) { listener.onRegistFinish(result); }; }.execute(); } 注册信息的接口: pulic interface OnRegistFinishListener{ void onRegistFinish(String result); }
使用HttpURLConnection进行网络访问
public void refistUser(Context context,final User user,final OnRegistFinshListener listener){ new Thread(){ public void run(){ try{ URL url=new URL("http://172.88.134.139:8080/ems/regist.do"); HttpURLConnection connection=(HttpURLConnection)url.openConnction(); connection.setRequestMethod("POST"); connection.setDoInput(true);//接受服务器响应 connection.setDoOutput(true);//向服务器提交内容 //为请求头中的内容作说明 设置参数 connection.setRequstProperty("Content_Type","application/x-www-from-urlencoded"); connection.connect(); //客户端向服务器提交参数 OutputStream out=connection.getOutputStream(); PrintWriter pw=new PrintWriter(out,true); String params=getParams(user); pw.print(params); pw.close(); //客户端获取服务器的响应内容 InputStream in=connection.getInputStream(); //使用Buffer读取响应内容 BufferedReader br=new BufferedRead(new InputStream(in)); final String result=br.readLine(); br.close(); //解析结果在主线程执行 new Handler(Looper.getMainLooper()).post(new Runnable(){ @Override public void run() { listener.onRegistFinish(result); } }); }catch(Exception e){ e.printStackTrace(); } }; }.start(); } 注册信息的接口: pulic interface OnRegistFinishListener{ void onRegistFinish(String result); }
相关文章推荐
- tcpdump讲解
- 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
- document.readyState和xmlhttp.onreadystatechange
- 学习笔迹——DisplayImageOptions,网络图片显示
- zeroc-ice网络请求使用介绍
- 网络封包分析工具Charles(接口抓取)
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件 【转】http://www.cnblogs.com/lxblog/archive/2013/01/09/2853056.html
- 22java中使用UDP TCP 协议做聊天工具
- 使用distcp并行拷贝大数据文件
- tomcat8设置https连接
- 使用nginx搭建http代理服务器
- [网络]Unity和PHP的Http数据交互
- 利用HttpClient的POST方式发起带参数的请求时,点击注册按钮无反应状态分析
- 在嵌入式学习过程中,很多人都有这样的困惑!——为什么要学习嵌入式技术?
- 【opencv】神经网络识别数字
- httpurlconnection_vegetables(httpurlconnection请求方式获取菜谱)
- 发布mvc遇到的HTTP错误 403.14-Forbidden解决办法
- Httpclient_星座查询
- mysql事务和锁InnoDB(转自http://www.cnblogs.com/zhaoyl/p/4121010.html)
- 报错 Error:Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradl