安卓与后台进行数据交互
来源:互联网 发布:网络赌钱输了好多 编辑:程序博客网 时间:2024/06/05 15:51
方式一:
public String updateRequest(String operation,String phone) { String strResult = null; HttpPost httpRequest =new HttpPost(CommonURL.UpdateURL); List <NameValuePair> params=new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("Operation",operation)); params.add(new BasicNameValuePair("Phone",phone)); try{ httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest); if(httpResponse.getStatusLine().getStatusCode()==200){ //取出回应字串 strResult=EntityUtils.toString(httpResponse.getEntity()); Pattern titleP = Pattern.compile("<body>(.*\r\n)*</body>"); Matcher matchedTitle = titleP.matcher(strResult); Boolean foundTitle = matchedTitle.find(); if(foundTitle){ strResult = matchedTitle.group(1); } }else{ Log.e("请求结果:", "请求失败!"); } }catch(ClientProtocolException e){ e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return strResult; }
方式二:
public String sendRequest(JSONObject object,String MyURL) { String jsonStr=null; try { URL url = new URL(MyURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("ser-Agent", "Fiddler"); conn.setRequestProperty("Content-Type", "application/json"); conn.setConnectTimeout(5 * 1000); // 包装并上传数据 OutputStream outputStream = conn.getOutputStream(); outputStream.write(URLEncoder.encode(object.toString(), "UTF-8").getBytes()); // 如果请求响应码是200,则表示成功 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { //获取服务器上的数据 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); //解码 jsonStr = URLDecoder.decode(in.readLine(), "UTF-8"); in.close(); conn.disconnect(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (SocketTimeoutException e) { jsonStr = "连接超时"; }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonStr; }
阅读全文
0 0
- 安卓与后台进行数据交互
- Android与web后台进行数据交互
- 安卓开发-后台与UI交互
- 安卓如何与后台交互流程
- PHP后台数据处理及与web端进行数据交互
- java后台与页面进行数据交互时产生乱码
- ajax与后台数据交互
- 前台与后台数据交互
- JS调用后台方法进行数据交互
- 安卓使用WebView与js进行交互
- 用AJAX与后台进行交互
- AngularJS如何与后台服务器进行交互
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 jsp页面
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet配置页面
- Ionic从零单排(五)——Ionic程序与Java后台进行数据交互
- 引用第三方进行Android前端与web后台的数据交互
- Android与web后台进行数据交互的三种方式
- Angular4 开发环境搭建
- 推荐几款实用的Android Studio 插件
- C#将字符串转换为整型的三种方法的总结
- android gps机制分析--定位数据HAL处理
- Linux下BLAST安装及BLAST使用
- 安卓与后台进行数据交互
- MySQL 入门常用命令大全(下)
- 请多指教
- 字符函数
- iFrame只要竖滚动条,不要横滚动条的方法
- spring 中JdbcTemplate的queryForObject、queryForMap为空返回错误情况的一个处理
- 关于DSP内存空间不足的问题
- delphi中表格单元格编辑运算符的小经验
- 布局线下移动观影体验,大画VR一体机打造二级移动院线