Android初级开发(九)——网络交互—HttpURLConnection
来源:互联网 发布:触摸屏一体机软件 编辑:程序博客网 时间:2024/06/12 20:53
一、使用HttpURLConnection的步骤
1、获取到HttpURLConnection的实例,并传入目标的网络地址,然后调用openConnection()方法
URL url = new URL("http://www.baidu.com");HttpURLConnection connection = (HttpURLConnection)url.openConnection();
2、设置HTTP请求所使用的方法GET和POST
GET:从服务器那里获取数据
之后再调用getInputStream()方法获取到服务器返回的输入流
connection.setRequestMethod("GET");InputStream in = connection.getInputStream();
POST:提交数据给服务器
每条数据都要以键值对的形式存在,数据与数据之间用“&”符号隔开
connection.setRequestMethod("POST");DataOutputStream out = new DataOutputStream(connection.getOutputStream());out.writeBytes("username=admin&password=123456");
3、调用disconnect()方法将HTTP连接关闭掉
connection.disconnect();
二、实例
需求:一个按钮用于发送HTTP请求,一个文本框用于将服务器返回的数据显示出来
1、效果图
2、布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="80dp" android:text="发送请求" android:textSize="25dp"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/response_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView></LinearLayout>
3、MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView responseText; Button sendRequset; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendRequset = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response_text); sendRequset.setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.send_request){ sendRequsetWithHttpURLConnection(); } } private void sendRequsetWithHttpURLConnection() { //开启线程来发起网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; BufferedReader reader = null; try { URL url = new URL("http://www.baidu.com"); connection = (HttpURLConnection) url.openConnection(); //提交数据给服务器的步骤 // connection.setRequestMethod("POST"); // DataOutputStream out = new DataOutputStream(connection.getOutputStream()); // out.writeBytes("username=admin&password=123456"); connection.setRequestMethod("GET"); //设置链接超时的毫秒数 connection.setConnectTimeout(8000); //设置读取超时的毫秒数 connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); //下面对获取到的输入流进行读取 reader = new BufferedReader(new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine())!=null){ response.append(line); } showResponse(response.toString()); }catch (Exception e){ e.printStackTrace(); }finally { if(reader != null){ try { reader.close(); }catch (IOException e){ e.printStackTrace(); } } if (connection != null){ connection.disconnect(); } } } }).start(); } //结果传入showResponse方法中 private void showResponse(final String s) { runOnUiThread(new Runnable() { @Override public void run() { //在这里进行UI操作,将结果显示到界面上 responseText.setText(s); } }); }}
4、声明网络权限
<uses-permission android:name="android.permission.INTERNET"/>
阅读全文
0 0
- Android初级开发(九)——网络交互—HttpURLConnection
- Android初级开发(九)——网络交互—WebView的使用
- Android初级开发(九)——网络交互—OkHttp
- Android初级开发(九)——网络交互—解析XML格式数据
- Android初级开发(九)——网络交互—解析JSON格式数据
- Android——网络通信(HttpUrlConnection)
- android网络应用3——HttpURLConnection
- Android 网络通信——HttpURLConnection
- android学习——网络访问HttpURLConnection
- Android中网络请求—HttpClient、HttpURLConnection
- android网络应用(二)——HttpURLConnection和HttpClient
- android 基础 网络交互 HttpClient HttpUrlConnection
- Android网络开发之HttpURLConnection
- 网络连接——HttpURLConnection
- 网络访问——HttpURLConnection
- Android开发初级(二)——开发环境搭建
- Android网络间接——HttpURLConnection.getResponseCode() 错误
- Android第九讲——网络(二) HttpUrlConnection
- 从一道网易面试题浅谈OC线程安全
- 1568:兔子繁衍问题
- 边界值分析法设计测试用例
- selenium之unittest的简单用法(一)
- Effective STL 20 Specify comparison types for associative containers of pointers
- Android初级开发(九)——网络交互—HttpURLConnection
- Batch Normalization 学习笔记
- av_register_all函数分析
- 在字符串中插入另一个字符串方法二
- 【年度总结】万里赴戎机,关山度若飞
- Jquery里面的jsonp
- 【转】CSS块级元素和行内元素
- 欢迎使用CSDN-markdown编辑器
- 解决ie8,7,6不支持html5标签的问题