Http的Get和Post提交方式的自定义工具类
来源:互联网 发布:淘宝赚钱软件 编辑:程序博客网 时间:2024/06/06 03:41
Get和Post提交方式的自定义工具类:
Get方式提交的方法工具类:
**
* http访问网络的get和post方式的封装类
* Created by Administrator on 2017/3/13.
*/
public class HttpUtil {
/**
* 静态get方法工具类,直接用类名调用
* 调用自定义读取连接返回结果的封装类的方法
*/
public static String httpGet(String url, Map<String,Object> map){ //url的拼接操作url+"?key=value&" try { //调用一个自定义的解析参数map获取username和password的方法 String data=parsePamars(map); String strurl=url+"?"+data; URL newUrl=new URL(strurl); HttpURLConnection conn= (HttpURLConnection) newUrl.openConnection();//打开连接 conn.setRequestMethod("GET");//设置请求方式,大写的GET //调用自定义获取请求结果的封装方法 String result=readRusult(conn); return result; }catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null;
Post方式提交的方法工具类:
public static String httpPost(String url,Map
解析Map结合获取data数据的方法:
public static String parsePamars(Map<String ,Object> map){ String data=""; if(map!=null){ Set<String> set= map.keySet();//遍历map中的健,找到对应的值 for (String key:set) { String value= map.get(key).toString();//取得值 data=data+key+"="+value+"&";//最后会多出一个&符号 } if(!TextUtils.isEmpty(data)){ data= data.substring(0,data.length()-1);//将最后一个&符号去掉 } } return data;
读取CONN结果的方法:
public static String readRusult(HttpURLConnection conn){ try { int code= conn.getResponseCode(); if(code==200){//请求成功,读取结果 InputStream is=conn.getInputStream(); InputStreamReader inreader=new InputStreamReader(is); BufferedReader reader=new BufferedReader(inreader); String line=""; StringBuffer buffer=new StringBuffer(); while ((line=reader.readLine())!=null){ buffer.append(line); } //关闭流,断开连接 reader.close(); inreader.close(); is.close(); conn.disconnect(); return buffer.toString(); } } catch (IOException e) { e.printStackTrace(); } return null;}
MainAcvitity中调用工具类:
public class MainActivity extends AppCompatActivity {
private EditText et_user,et_password;
private Button btn_login;
private MyThread thread=null;
private MyHandle handle=null;
private String url=”http://192.168.15.114:8080/HttpTest/Login”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
handle=new MyHandle();//实例化控件时候也实例化handle
et_user= (EditText) findViewById(R.id.et_user);
et_password= (EditText) findViewById(R.id.et_password);
btn_login= (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//①实例化控件,给按钮添加点击事件,点击按钮启动子线程
if (thread==null){
thread=new MyThread();
thread.start();
}
}
});
}
/** * ②自定义子线程 */class MyThread extends Thread{ //③重写run方法 //访问网络,进行登录,把登录的结果利用handle发送到主线程,写一个访问网络的方法 @Override public void run() { super.run(); //⑤调用访问网络的方法,获取结果值 String result= loginGet(); String result=loginPost(); Map<String ,Object> map=new HashMap<>(); map.put("username",et_user.getText().toString()); map.put("password",et_password.getText().toString()); String result=HttpUtil.httpGet(url,map); String result=HttpUtil.httpPost(url,map); //⑥子线程向主线程发送message,需要自定义handle Message message= handle.obtainMessage(1,result); handle.sendMessage(message); //发送过消息,线程置空,方便下次使用 thread=null; }}
- Http的Get和Post提交方式的自定义工具类
- HTTP POST和GET的提交方式
- 表单的提交方式POST和GET
- Get和Post的提交方式
- 表单的提交方式:get和post
- get提交方式和post提交方式的区别
- HTTP get和post方式的区别
- Http的Get和Post方式详解
- http的post和get请求方式
- 浅谈post提交和get提交方式的区别
- HTTP的两种提交请求的方式get和post
- java http请求的get和post方法工具类
- Http Post、Get提交工具
- Http访问方式GET/POST的测试工具Postman
- Http访问方式GET/POST的测试工具Postman
- 网页提交方式post和get的区别和联系
- GET和POST提交数据方式的区别和使用
- GET和POST提交数据方式的区别和使用
- 多线程的面试题迅雷
- 贪财的富翁(作业)
- Servlet初识(JavaWeb学习笔记四)
- Android代码混淆
- c3p0简单实现方式
- Http的Get和Post提交方式的自定义工具类
- 删除字符串中出现次数最少的字符
- jQuery.extend 函数使用详解
- Win10系统安装教程
- 读入优化
- zookeeper实现异地容灾模块
- CSS3-鼠标经过图片旋转、缩放、还原
- 测试不同格式下depth buffer的精度
- 人民政协报:倾尽丹青展雄魂——记全国政协委员汪国新