使用Andnroid的post请求
来源:互联网 发布:java lock实现原理 编辑:程序博客网 时间:2024/05/16 08:44
public static String commitPost(String path, Map<String, String> params, String encoding) { StringBuffer sb = new StringBuffer(); try { //将map转化为网络数据发送 if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()).append("="); sb.append(URLEncoder.encode(entry.getValue(), encoding)); sb.append("&"); } sb.deleteCharAt(sb.length() - 1); } byte[] data = sb.toString().getBytes(); URL url = new URL(path); //获取httpURLConection对象 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //获取输入流 OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); //获取相应 if (conn.getResponseCode() == 200) { byte[] buffer =new byte[1024]; int r; StringBuffer contextBuffer = new StringBuffer(); while ((r = conn.getInputStream().read(buffer)) != -1) { contextBuffer.append(new String(buffer, 0, r)); } return contextBuffer.toString(); } return ""; } catch (Exception e) { return "请求内部出现异常!"; } //alt+command+l refresh code }
使用HttpURLConnection获取相关的输入流和输出流即可。
测试:
private Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==200){ Bundle bundle=msg.getData(); Toast.makeText(TestPost.this,bundle.getString("info"),Toast.LENGTH_SHORT).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_post); } public void testPost(View view) { new Thread(new Runnable() { @Override public void run() { Map<String,String> map=new HashMap<String, String>(); map.put("phone","13660800712"); map.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); String result=SetPostUtil.commitPost("http://apis.juhe.cn/mobile/get?", map, "utf-8"); Log.i("result====>",result); Message message=Message.obtain(); message.what=200; Bundle bundle=new Bundle(); bundle.putString("info",result); message.setData(bundle); mHandler.sendMessage(message); // Toast.makeText(TestPost.this,result,Toast.LENGTH_SHORT).show(); } }).start(); }
虽然,这些没有什么用,但是很基础适合悠闲时,coding
0 0
- 使用Andnroid的post请求
- 【iOS-网络】使用异步的post请求
- AJAX请求 $.post方法的使用
- 使用post的方式发送http请求
- Volley的使用(二) post请求
- GET 与POST的请求使用
- AJAX请求 $.post方法的使用
- 使用xml格式ajax的post请求
- 简述OkHttp的使用:get、post请求
- AJAX请求 $.post方法的使用
- AJAX请求 $.post方法的使用
- AJAX请求 $.post方法的使用
- get post 请求 使用的场景
- 使用原生的Aajax发送Post请求
- IOS 中使用 POST请求与GET请求的区别
- java使用httpclient封装post请求和get的请求
- 使用POST方式请求
- js使用post请求
- 【实验-视频过程】闪回数据库Flashback database
- 剑指offer—孩子们的游戏(圆圈中最后剩下的数)
- 内核学习-中断级别,分页内存,用户模式以及内核模式
- markdown使用LaTeX语法编写数学公式
- Codeforces Round #271 (Div. 2) F - Ant colony 线段树 + GCD
- 使用Andnroid的post请求
- 如何将GDB中需要的调试信息输出到文件
- xeCJK时的警告:xeCJK warning: "CJKfamily-Unknown"
- mysql中profile用法
- HDU 1423 裸LCIS
- ArrayList/LinkedList/Vector
- 想开发iPhone App?来看看这个6个适合新手的项目吧
- HDU5496--Beauty of Sequence(水题)
- Cocos2dx控件 Text和TextField