需求:用户登录(name:用户名,pwd:密码)
(一)HttpGet :doGet()方法
//doGet():将参数的键值对附加在url后面来传递
public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{
//服务器 :服务器项目 :servlet名称
String path="http://192.168.5.21:8080/test/test";
String uri=path+"?name="+name+"&pwd="+pwd;
//name:服务器端的用户名,pwd:服务器端的密码
//注意字符串连接时不能带空格


String result="";
HttpGet httpGet=new HttpGet(uri);
HttpResponse response=new DefaultHttpClient().execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity=response.getEntity();
result=EntityUtils.toString(entity, HTTP.UTF_8);
}
return result;
}
(二)HttpPost :doPost()方法
//doPost():将参数打包到http报头中传递
public String getReultForHttpPost(String name,String pwd) throws ClientProtocolException, IOException{
//服务器 :服务器项目 :servlet名称
String path="http://192.168.5.21:8080/test/test";
HttpPost httpPost=new HttpPost(path);
List<NameValuePair>list=new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name", name));
list.add(new BasicNameValuePair("pwd", pwd));
httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
String result="";
HttpResponse response=new DefaultHttpClient().execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity=response.getEntity();
result=EntityUtils.toString(entity, HTTP.UTF_8);
}
return result;
}

更多相关文章

  1. 万字长文带你了解最常用的开源 Squid 代理服务器
  2. Nginx系列教程(一)| 手把手教你在Linux环境下搭建Nginx服务
  3. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  4. Android中使用HttpGet和HttpPost访问HTTP资源
  5. Android中MQTT的使用
  6. Android网络请求
  7. Android之记住密码与自动登陆实现
  8. Android(安卓)ListView 分页功能,包含前端分页,服务器分页
  9. jenkins搭建及基础配置

随机推荐

  1. Android中,单位dp、sp、px互相转换工具类
  2. Android(安卓)仿微信朋友圈发动态功能(相
  3. android源码分析流程-init.c
  4. Android(安卓)Studio3.0升级gradle遇到的
  5. Android更新完ADT后遇到的问题
  6. android studio 更改 app 包名和包的长度
  7. OSChina 技术周刊第十期,每周技术抢先看!
  8. android 退出应用方法一:点击两次退出应用
  9. Android(安卓)笔记5---事件处理和手势
  10. android屏幕页面实现滚动,页面跳转