HttpUrlConnection(GET方式)
来源:互联网 发布:ps4合金装备vb 编辑:程序博客网 时间:2024/05/29 07:36
一.
基本知识点:HttpUrlConnection有两种网络请求方式,GET和POST。下面我会把两种方式都写出来。
写之前我要说由于我不会弄服务器,所以我的链接是使用的天气预报的链接,反正本来就是来练手,不要在意太多,知道了使用方法,相信也就能举一反三。
1.GET方式:
1.1首先我们设置权限哦
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" />
1.2我的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit" /> <RadioGroup android:layout_width="match_parent" android:layout_height="55dp" android:layout_below="@+id/edit" android:orientation="horizontal" android:id="@+id/rg" android:weightSum="4"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:text="①GET" android:id="@+id/one" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:text="①POST" android:id="@+id/two" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:text="②GET" android:id="@+id/three" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:text="②POST" android:id="@+id/four" android:layout_weight="1"/> </RadioGroup> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/text" android:layout_below="@+id/rg"/></RelativeLayout>
其中:
①GET表示HttpUrlConnection的GET方式
①POST表示HttpUrlConnection的POST方式
②的按钮不用管,还没写呢!!!
初始化那些基本步骤我就不说了
1.3使用线程,进行访问
private class OneThread implements Runnable { InputStream is; BufferedReader br; @Override public void run() { getPath(); try { //通过路径得到URL对象 url = new URL(todayWeather); //打开服务器 conn = (HttpURLConnection) url.openConnection(); //连接服务器 conn.connect(); //得到输入流 is = conn.getInputStream(); //封装 br = new BufferedReader(new InputStreamReader(is)); String line = null; //将数据存储在StringBuffere中 StringBuffer sb = new StringBuffer(); while ((line = br.readLine()) != null) { sb.append(line); } sb.append("\n" + "\nWays:HttpUrlConnection-GET"); content = sb.toString(); //使用消息传递机制,更改主线程UI Message message = new Message(); message.what = 1; handler.sendEmptyMessage(message.what); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
其中getPath()方法是得到天气预报接口,将它传给todayWeather了。
代码在这:
public void getPath() { String todayStart = "http://api.k780.com:88/?app=weather.today&weaid="; String todayCity = edit.getText().toString(); String todayEnd = "&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"; todayWeather = (todayStart + todayCity + todayEnd); }
不在子线程中使用的话,程序会崩掉哦!!!
感觉自己代码中注释挺详细的,这里就不再解释了。
1.4由于要更改UI,即把数据放到TextView中,我们知道,更改UI是只能主线程做的哦,所以我们要使用消息传递机制,告诉主线程,你要更改UI啦,为此我们写一个类继承Handler,在里面处理。
class MyHandler extends Handler { @Override public void handleMessage(Message msg) { if (msg.what == 1) { text.setText(content); } else { text.setText("出错了"); } } }
这样我们就实现了GET方法的网络请求了,不过最近脑袋不太好使,漏掉什么的话大家多包涵吧。
POST在下一篇写吧,完整代码也在下一篇给吧(两种方法一起给出),Bye。
0 0
- HttpUrlConnection(GET方式)
- HttpURLConnection--HttpURLConnection的Get请求方式
- HttpURLConnection POST/GET 处理方式
- HttpUrlConnection的get请求方式
- HttpURLConnection get post 方式请求 (笔记)
- HttpURLConnection GET 方式 请求网络数据
- HttpURLConnection的GET和POST请求方式
- HttpURLConnection方式、HttpClient方式发送get、post数据给服务端
- HttpUrlConnection以GET方式和POST方式请求服务器
- 使用HttpURLConnection采用get方式或post方式请求数据
- java发送post和get请求(1)---HttpURLConnection方式
- Android中的post和get请求(HttpURLConnection)方式
- android 使用HttpURLConnection方式提交get/post请求
- 使用HttpURLConnection采用get方式请求数据-----乱码问题
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- 采用HttpURLConnection的GET方式实现登陆案例
- HttpURLConnection HttpClient AsyncHttpClient 实现Post或者Get方式登陆
- 使用HttpUrlConnection的GET方式从网上下载数据
- 走遍亚洲 —— 印度半岛
- 2016 Multi-University Training Contest 2 Keep On Movin
- UICollectionView的基本概念与使用
- 实现鼠标滑动,让某个div固定在最上方
- hjr-c#实习日记(四):关于路由
- HttpUrlConnection(GET方式)
- tomcat配置文件server.xml详解
- LR(1) 有限状态机的压缩
- 使用AndFix实现Android热修复
- L1-030. 一帮一-PAT团体程序设计天梯赛GPLT
- java以命令行启动调试模式
- awk截取字串的方法
- rightclick
- L1-031. 到底是不是太胖了-PAT团体程序设计天梯赛GPLT