Android笔记(二十二) HttpURLConnection
2015-09-07 09:05
549 查看
一、 HttpURLConnection用法
获取 HttpURLConnection实例
设置 HTTP 请求所使用的方法
调用 getInputStream()方法获取服务器返回的输入流
对输入流进行读取
将连接关闭
二、具体实例
1.建立布局
2.添加权限
3.MainActivity
获取 HttpURLConnection实例
设置 HTTP 请求所使用的方法
调用 getInputStream()方法获取服务器返回的输入流
对输入流进行读取
将连接关闭
二、具体实例
1.建立布局
<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="wrap_content" android:text="Send Request" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
2.添加权限
uses-permission android:name="android.permission.INTERNET"
3.MainActivity
public class MainActivity extends ActionBarActivity implements OnClickListener { public static final int SHOW_RESPONSE = 0; private Button button; private TextView responseText; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; responseText.setText(response); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response); button.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.send_request) { sendRequestWithHttpURLConnection(); } } private void sendRequestWithHttpURLConnection() { new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://www.baidu.com"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } Message msg = Message.obtain(); msg.what = SHOW_RESPONSE; msg.obj = response.toString(); handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }).start(); } }
相关文章推荐
- Linux 网络配置
- 根文件系统的挂载过程 http://blog.sina.com.cn/s/blog_67e2ef6a0100s8kf.html
- http tcp ip 模型解释 ngix expoll select apache
- 网络数据挖掘程序的设计
- 黑马程序员-Java基础:网络编程
- java之--------socket编程(TCP)服务器请求与客户端
- HttpURLConnection post请求
- hdu 4280 Island Transport 【网络流+优化】
- 网络部分(一)
- 菜鸟学matlab之智能算法(2)——————————BP神经网络算法
- 移动应用网络优化
- 计算机网络面试题
- HttpServletResponse 小知识
- SOAP and .net services for Apache HTTP Server
- iOS开发之加载大量网络图片优化
- 浏览器相关技术介绍
- 使用 libevent 和 libev 提高网络应用性能——I/O模型演进变化史
- Andorid 分别用AsyncTask和Handler下载网络图片
- AS3.0中用于网络通信的类总结
- 计算机网络各层协议【转载】