用HttpURLConnection访问网络
来源:互联网 发布:java class a的注释 编辑:程序博客网 时间:2024/06/09 16:46
本次要实现的功能为
1.用HttpURLConnection接收网络数据
2.用HttpURLConnection发送数据给网络,并获得网络响应的数据。
1.流程:用HttpURLConnection接收网络数据
第一步: 新建一个子线程来写通信代码
第二步: 用URL类处理访问地址
第三步: 用URL类里的openConnection()方法建立网络连接
第四步: 设置请求参数,比如:
用HttpURLConnetion类里的setRequestProperty()设置消息头,编码格式等等(可以省略);
用HttpURLConnetion类里的setConnectTimeout()设置网络连接时延迟时间(可以省略);
用HttpURLConnetion类里的setReadTimeout()设置读取网络数据时延迟时间(可以省略);
用HttpURLConnetion类里的setRequestMethod(“POST”)设置为post传输模式(可以省略);
用HttpURLConnetion类里的setRequestMethod(“GET”)设置为get传输模式(可以省略);
第五步: 用输入流获得网络数据
调用HttpURLConnection里的getInputStream()方法获得网络数据流
第六步: 用Handler处理获得的数据
第七步: 在AndroidManifest.xml文件中添加访问网络权限
<uses-permission android:name="android.permission.INTERNET" />
2.流程:用HttpURLConnection发送数据给网络并获得响应数据
第一步: 新建一个子线程来写通信代码
第二步:用URL类处理访问地址
第三步:用URL类里的openConnection()方法建立网络连接
第四步:设置请求参数
用HttpURLConnection里的setDoOutput(true)设置为可上传模式(必须要有);
用HttpURLConnetion类里的setRequestMethod(“POST”)设置为post传输模式(与上面的必须要有一个);
用HttpURLConnetion类里的setRequestProperty()设置消息头、编码格式等等(可以省略);
用HttpURLConnetion类里的setConnectTimeout()设置网络连接时延迟时间(可以省略);
用HttpURLConnetion类里的setReadTimeout()设置读取网络数据时延迟时间(可以省略);
第五步:用输出流发送数据
调用HttpURLConnection里的getOutputStream()方法发送数据流给网络,发送完后要用flush()清除缓存。
第六步:用输入流获得网络响应的数据
调用HttpURLConnection里的getInputStream()方法获得网络数据流
第七步:用Handler处理接收到的数据
第八步:在AndroidManifest.xml文件中添加访问网络权限
<uses-permission android:name="android.permission.INTERNET" />
UI代码:
<LinearLayout 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" android:orientation="vertical" tools:context="com.example.adminis.wedapp.URLConnectionActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginLeft="30dp" android:layout_marginTop="20dp" android:text="用户名:" android:textSize="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/ed_User" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="bottom"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginLeft="30dp" android:layout_marginTop="20dp" android:text="密码 :" android:textSize="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/ed_Password" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="bottom"/> </LinearLayout> <Button android:text="发送数据" android:textSize="30dp" android:layout_marginTop="30dp" android:layout_gravity="center" android:id="@+id/btn_SendData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendData_OnClick"/> <Button android:text="接收数据" android:textSize="30dp" android:layout_marginTop="30dp" android:layout_gravity="center" android:id="@+id/btn_GetData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getData_OnClick"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scrollView" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView" /> </ScrollView></LinearLayout>
Java代码:
public class URLConnectionActivity extends Activity { private BufferedReader in; private TextView textView; private StringBuilder s; android.os.Handler handler = new android.os.Handler(){ //用hander去获得数据 @Override public void handleMessage(Message msg) { textView.setText(s); //让hander处理数据 super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_urlconnection); textView = (TextView) findViewById(R.id.textView); } //点击按钮发送数据(发送UI上得到的用户名和密码,用来登录某网址,并把网址的响应数据返回显示出来) public void sendData_OnClick(View view){ final EditText userEd = (EditText) findViewById(R.id.ed_User); final EditText passEd = (EditText) findViewById(R.id.ed_Password); new Thread(new Runnable() { //新建一个子线程来发送数据 PrintWriter out = null; //字符流 BufferedReader in = null; //输入流 @Override public void run() { //一定要用线程,因为这个响应速度需要一定时间 try { //用URL类处理访问地址 这里做实验应该用自己的网页里面获取用户名和密码做响应.或者是得到某一个网址里面的输入参数名字 URL url = new URL("http://192.168.10.108:8080/follow1/login.jsp"); //建立网络连接 //使用URLConnection模式的话就这里不一样 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection1.setRequestMethod("POST"); //传输模式和上传模式必须要有一个 //设置请求参数 connection.setDoOutput(true); //设置可上传模式(必须) out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8")); //由于都是文字,所以可以使用字符流,再加上一个缓冲流 //这里的name和pass分别对应的是网页代码里的变量名字,必须是一致的.*** out.print("name=" + userEd.getText().toString() + "&pass=" + passEd.getText().toString()); //发送给网页相应的数据,这里需要得到网页里面的登录和密码的变量,名称是怎样就怎样写 out.flush(); //清空一下缓存 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); //用输入流获得网络相应的数据 s = new StringBuilder(); //把读到的数据统一保存一下 String line; while ((line=in.readLine())!=null){ s.append(line); } handler.sendEmptyMessage(0x123); //用handler处理得到的数据 } catch (Exception e) { e.printStackTrace(); }finally { //最后需要关闭资源 if(out!=null){ out.close(); } if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }).start(); } //接收按钮下载网络数据 百度 public void getData_OnClick(View view){ new Thread(new Runnable() { //新建一个子线程来写通信代码 @Override public void run() { try { URL url = new URL("http://www.baidu.com"); //用URL类处理访问地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //建立网络连接 in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//用字符流输入流获得网络数据 s = new StringBuilder(); //用来暂时保存数据 String line; while ((line = in.readLine()) != null){ //读的每一行不为null,就添加到暂时保存的容量中 s.append("\n"+line); } handler.sendEmptyMessage(0x123); //用hander去获得数据 } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); //关闭输入流 } catch (IOException e) { e.printStackTrace(); } } } }).start(); }}
如果是使用URL的话,就把HttpURLConnection改为URLConnetion就行了,注意发送访问的网站必须是知道网址的输入参数变量名,不能随便发送
- 用HttpURLConnection访问网络
- Android 用HttpURLConnection访问网络
- HttpClient已过时,访问网络用HttpURLConnection
- HttpClient已过时,访问网络用HttpURLConnection
- HttpClient已过时,访问网络用HttpURLConnection
- HttpURLConnection(访问网络)
- 使用HttpURLConnection访问网络
- HttpURLConnection访问网络
- 使用HttpURLConnection访问网络
- android HttpURLConnection网络访问
- 使用HttpURLConnection访问网络
- 网络访问之HttpURLConnection
- 使用HttpURLConnection访问网络
- 网络访问之HttpURLConnection
- 【封装】异步HttpURLConnection网络访问
- 网络访问——HttpURLConnection
- Android 之 HttpURLConnection 访问网络
- 用HttpURLConnection即时访问网络图片的小Demo
- 一篇SSM框架整合友好的文章(一)
- Html手记:div和span标签
- uml图的五种关系
- Redis主从配置
- 一篇SSM框架整合友好的文章(二)
- 用HttpURLConnection访问网络
- Codeforces Round #385 (Div. 2)
- 第一篇博。。
- C语言代码
- java核心技术卷一学习 2016.11.27/3
- MapReduce Commands Guide
- AtomicInteger
- 一篇SSM框架整合友好的文章(三)
- msg.c