HttpURLConnection的POST请求
来源:互联网 发布:盛发软件 编辑:程序博客网 时间:2024/06/05 04:14
手机归属地查询(整个类)
package com.example.day03_post;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { protected static final int SUCCESS = 1; protected static final int FAIL = 0; private EditText ed_phonenum; private TextView tv_showdata; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: String cdata = (String) msg.obj; tv_showdata.setText(cdata); break; case 0: String info = (String) msg.obj; // 吐司要在主线程中写,写在子线程中报错*********** Toast.makeText(MainActivity.this, info, 0).show(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { ed_phonenum = (EditText) findViewById(R.id.ed_phonenum); tv_showdata = (TextView) findViewById(R.id.tv_showdata); } public void loadphonewonership(View v) { new Thread() { private HttpURLConnection openConnection; public void run() { String phmum = ed_phonenum.getText().toString(); // http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 try { // 实例化URL,设置网址 URL url = new URL( "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm"); // 实例化HttpURLConnection.(打开连接) openConnection = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST openConnection.setRequestMethod("POST"); // 是否向服务器发送数据 openConnection.setDoOutput(true); StringBuffer sb = new StringBuffer(); sb.append("tel=").append(phmum); byte[] buffer = sb.toString().getBytes(); // 向服务器发送数据(字节) openConnection.getOutputStream().write(buffer); // 如果请求成功 if (openConnection.getResponseCode() == 200) { // 得到请求到的数据 InputStream inputStream = openConnection .getInputStream(); ByteArrayOutputStream arrayout = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int length = 0; // 边读边写******************* while ((length = inputStream.read(b)) != -1) { arrayout.write(b, 0, length); } // 写完关流 arrayout.close(); // 以gbk形式写出来 String data = arrayout.toString("gbk"); Message message = Message.obtain(); message.obj = data; message.what = SUCCESS; // 向handle发送消息 handler.sendMessage(message); } else { // 请求数据失败,在此和 catch中都需要写所以封装成一个方法 requestfail(); } } catch (MalformedURLException e) { requestfail(); e.printStackTrace(); } catch (IOException e) { requestfail(); e.printStackTrace(); } finally {// 最后断开连接 if (openConnection != null) { openConnection.disconnect(); } } } private void requestfail() { Message mesage = Message.obtain(); mesage.what = FAIL; mesage.obj = "请求数据失败"; handler.sendMessage(mesage); }; }.start(); }}
0 0
- HttpURLConnection的POST请求
- HttpUrlConnection的post请求
- HttpURLConnection--HttpURLConnection的Post请求方式
- HttpURLConnection的post请求总结
- HttpURLConnection发出的post请求
- HttpURLConnection发出的post请求
- HttpURLConnection的Post请求方式
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- HttpURLConnection 的get跟post请求
- HttpURLConnection的get和post请求
- HttpURLConnection的GET和POST请求
- HttpURLConnection的get和post请求实例
- 【Java】HttpURLConnection,HttpClient 的 Get,Post 请求
- HttpURLConnection的POST请求加载数据图片
- HttpUrlConnection的get和post请求方法
- HttpURLConnection的GET和POST请求方式
- 可靠的HttpUrlConnection实现get/post请求
- Unity3D - 图形性能优化:优化着色器加载时间
- Codeforces710C【数学】
- ibatis_mybatis_hibernate_(1)区别介绍
- CVPR 2016 | 商汤科技论文解析:物体分割
- 微信小程序把玩(十一)icon组件
- HttpURLConnection的POST请求
- HTML初级部分实战练习-我的博客(table的 )
- 嵌入式开发linux系统管理
- LwIP有无操作系统的区别
- SSM + Shiro 整合(4)- 在 Web 项目中添加 Shiro
- setInterval()详解
- ajax 常用模板
- SQL语言的介绍(Oracle)(1)
- 字符串中包含双引号