HttpURLConnection的简单使用
来源:互联网 发布:seo要学多久 编辑:程序博客网 时间:2024/06/05 09:04
1:添加连接网络的权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>2:查看服务器是支持get请求方式 还是 post请求方式3:将输入流转换成字符串的工具类,请求数据时会用到public class StreamToos { public static String readFromNetWork(InputStream is){ try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1){ baos.write(buffer,0,len); } return baos.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }}get请求/** * 通过HttpURLConnection从网络获取天气数据 get方式 * 聚合数据---全国天气预报接口 */public class MainActivity extends AppCompatActivity { private TextView text_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text_1 = (TextView) findViewById(R.id.text_1); } //点击事件 获取天气数据 public void httpGetWeather(View view){ //读取网络数据是耗时操作 放在子线程 new Thread(){ @Override public void run() { final Weather weather = getWeatherByGet(); //在主线程设置文本内容 runOnUiThread(new Runnable() { @Override public void run() { text_1.setText(weather.toString()); } }); } }.start(); } //用get方式获取天气数据 public Weather getWeatherByGet(){ try { //创建url get请求方式 将请求的数据参数全部写在网址上 如下: //其中http://v.juhe.cn/weather/index是接口地址 //请求内容以?分隔 请求参数以&分隔 //参数详细信息 看服务器(聚合数据)的参考信息 有必填项 有选填项 //format=2&cityname=北京& //key=01b3d0d1bb6251f441ffe18a07ad655c 申请接口的Appkey URL url = new URL("http://v.juhe.cn/weather/index?format=2&cityname=北京&key=01b3d0d1bb6251f441ffe18a07ad655c"); //建立连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置读取方式 connection.setRequestMethod("GET"); //设置与服务建立的连接时间 connection.setConnectTimeout(5000); //设置读取网络资源的时间 一般5s connection.setReadTimeout(5000); //服务器响应 得到返回码 int responseCode =connection.getResponseCode(); //HttpURLConnection.HTTP_OK其实等于200 这是HttpURLConnection类定义的常量 if(responseCode==HttpURLConnection.HTTP_OK){ //得到返回的数据 输入流 InputStream is = connection.getInputStream(); //通过工具类 将输入字节流转换成字符串 String json = StreamTOString.readFromNetWork(is); //通过Debug模式 将json串的值取出 然后创建bean类 通过gson插件 解析出bean类 Gson gson = new Gson(); Weather weather = gson.fromJson(json, Weather.class); return weather; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }}post请求/** * 查询菜谱信息 聚合数据-->菜谱大全 * 点击button时,将查询的内容显示在textview上 * */public class MainActivity extends AppCompatActivity { private TextView textView; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text_1); editText = (EditText) findViewById(R.id.edittext); } //点击事件 查询 public void qurry(View view){ new Thread(){ @Override public void run() { super.run(); //调用 final String s = httpUrlConnectionByPost("红烧肉"); //在主线程给text view赋值 runOnUiThread(new Runnable() { @Override public void run() { textView.setText(s); } }); } }.start(); } //post查询 public String httpUrlConnectionByPost(String name){ try { //创建url 服务器接口地址 URL url = new URL("http://apis.juhe.cn/cook/query.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置与服务器建立连接时间 connection.setConnectTimeout(5000); //设置读取网络数据的时间 connection.setReadTimeout(5000); //设置请求方式 connection.setRequestMethod("POST"); //请求的数据 OutputStream os = connection.getOutputStream(); os.write(("menu="+name+"&key=de558ae7e2e2a24fc1f013fbb3327ae1").getBytes()); PrintWriter writer = new PrintWriter(os); writer.flush(); if (connection.getResponseCode() == 200){ //得到输入流 InputStream is = connection.getInputStream(); //通过工具类 将输入流转换成字符串 字符串为json串 String s = StreamTOString.readFromNetWork(is); return s; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }}
0 0
- HttpUrlConnection的简单使用
- HttpURLConnection的简单使用
- HttpURLConnection的简单使用
- Android HttpURLConnection的简单使用
- WebView,HttpURLConnection,HttpClient的简单使用
- HttpURLConnection 跟HttpClick 简单的使用
- HttpUrlConnection的简单使用--get和post的简单使用
- 简单的HttpURLConnection-Get
- WCF服务端与使用HttpURLConnection的Android客户端简单示例
- android 使用httpURLConnection post方式访问接口的简单工具包
- HttpURLConnection的使用
- HttpURLConnection的使用
- HttpURLConnection的使用
- HttpURLConnection的使用
- HttpURLConnection的使用
- HttpURLConnection的使用
- HttpURLConnection的使用
- HttpURLConnection的使用
- git 下载代码问题处理
- Kubernetes 集群安装指南
- easyui datagrid列中使用tooltip
- 需求工程
- POJ 1789
- HttpURLConnection的简单使用
- 二分快速幂
- go中的main函数和init函数
- 内部排序算法5(基数排序)
- 各大音乐网站接口
- 设计模式03:抽象工厂
- springmvc使用拦截器
- TensorsFlow学习笔记4----面向机器学习专家的深度MNIST教程(Deep MNIST for Experts)
- android 广播的使用