Android向Web服务器发送Post请求并返回数据
来源:互联网 发布:ubuntu 系统log 编辑:程序博客网 时间:2024/04/30 14:42
1.android 向web服务器发送post请求并获取结果,因为 需要访问到网络必须要有权限,先在AndroidManifest.xml中加入如下配置:
<uses-permission android:name="android.permission.INTERNET"/>
2.发送post请求并获取结果的activity 代码如下(结果返回1(成功)或-1(失败0)):
btnOK.setOnClickListener(new OnClickListener(){ @Overridepublic void onClick(View view) {String url="http://192.168.123.7:8900/Login.aspx";HttpPost httpRequest=null;List<NameValuePair> params=null;HttpResponse httpResponse=null;//建立HttpPost链接httpRequest=new HttpPost(url);//Post操作参数必须使用NameValuePair[]阵列储存params=new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("domain",domain.getText().toString()));params.add(new BasicNameValuePair("uid",uid.getText().toString()));params.add(new BasicNameValuePair("pwd",pwd.getText().toString()));try{ //发送Http RequesthttpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //取得Http ResponsehttpResponse=new DefaultHttpClient().execute(httpRequest);//若状态码为200if(httpResponse.getStatusLine().getStatusCode()==200) {//获得返回的数据 String strResult=EntityUtils.toString(httpResponse.getEntity()); if(strResult.equalsIgnoreCase("1")) { // openDialog("登入成功!");new AlertDialog.Builder(Login.this).setTitle("提示").setMessage("登入成功!").setPositiveButton("确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// 跳转到另一个Acitivity并传值Intent intent=new Intent();intent.putExtra("curUserId",domain.getText().toString()+"/"+ uid.getText().toString()); intent.setClass(Login.this, Holiday.class); Login.this.startActivity(intent);}}).show(); } else if(strResult.equalsIgnoreCase("0")) { openDialog("您输入的信息有误!"); } }else{openDialog("Error!"); }}catch(Exception e){e.printStackTrace();}}});
- Android向Web服务器发送Post请求并返回数据
- Android向Web服务器发送Post请求并返回数据
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
- AJAX向服务器发送请求并返回json数据
- post请求向服务器发送数据
- 向Web站点发送GET请求,POST请求,并从Web站点取得相应数据示例
- Android向Web站点发送GET请求、POST请求
- 向服务器发送请求,并解析返回的XML
- Android--使用Http向服务器发送请求并取得返回结果,下载图片
- Android--使用Http向服务器发送请求并取得返回结果,下载图片
- c++ HTTP POST请求 向服务器发送json数据
- libcurl HTTP POST请求向服务器发送json数据
- Android开发使用POST方式向服务器请求和发送数据
- 模拟http发送post请求并返回数据
- Android使用HttpURLConnection向服务器发送post和get请求
- 向指定 URL 发送GET,POST方法的请求(传入指定url和参数,服务器返回字符串数据)
- android新增修改数据 采用Post向服务器端发送请求
- 向Web站点发送GET请求、POST请求,并从Web站点取得响应
- 国外主流PHP框架比较 (CodeIgniter、CakePHP、ZendFramework、Symfony)
- RHEL LVM详解
- 在VS2005 /VS2008下调试应用程序的两种途径
- 在设计数据库时需要注意哪些?
- 新浪微博布局学习——妙用TabHost
- Android向Web服务器发送Post请求并返回数据
- java生成MD5校验码及算法实现
- Linux添加硬盘并分区格式化
- Struts2读取配置文件的顺序
- wxGIS介绍
- Ubuntu添加开机自动启动程序的方法
- EJB常用生成数据库注解
- java生成MD5校验码及算法实现
- php 和 perl 针对文件 的md5