android中使用http协议访问网络
来源:互联网 发布:amazing frog 知乎 编辑:程序博客网 时间:2024/06/06 03:35
在android应用中可以通过HttpURLConnection类来使用http协议访问网络,大概的流程是这样的。
1、获取HttpURLConnection的实例,通过一个URL对象调用openConnection()方法来获得
2、通过HttpURlConnection实例的setRequestMethod()方法设置http请求所用的方法,一般为GET或POST
3、使用HttpURLConnection实例的getInputStream()方法来获取服务器返回的输入流
4、调用HttpURLConnection实例的disconnect()来关闭http连接
接下来举一个小例子,新建一个HttpURLConnection项目:
activity_main.xml中的代码如下:
<?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/sendRequest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Requset" android:textAllCaps="false"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/responseData" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView></LinearLayout>
布局中添加了一个按钮用于触发发送http请求事件,ScrollView控件中放置一个TextView控件用于显示从服务器接受到的响应文本信息。
AndroidManifest.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tangliang.httpurlconnection"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
由于程序要访问网络,所以这里还是要申请INTERNET权限
MainActivity中的代码如下:
package com.tangliang.httpurlconnection;import android.renderscript.ScriptGroup;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.view.menu.ExpandedMenuView;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class MainActivity extends AppCompatActivity { private TextView responseData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendRequest = (Button) findViewById(R.id.sendRequest); responseData = (TextView) findViewById(R.id.responseData); sendRequest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendRequestWithHttpURLConnection(); } }); } private void sendRequestWithHttpURLConnection(){ new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; BufferedReader reader = null; try{ URL url = new URL("http://blog.csdn.net/a18779148177"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); 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 (Exception e){ e.printStackTrace(); } } if(connection != null){ connection.disconnect(); } } } }).start(); } private void showResponse(final String response){ runOnUiThread(new Runnable() { @Override public void run() { responseData.setText(response); } }); }}
MainActivity中有几点需要注意,一是在接受响应完成后要关闭流和HttpURLConnection对象,二是不能在UI线程中进行耗时操作,否则程序会崩溃,三 是不能在子线程中更新UI,必须要切换回UI线程中才能更新UI。
程序运行,并按下发送请求按钮后的结果图如下:
阅读全文
1 0
- android中使用http协议访问网络
- Android学习使用HTTP协议访问网络
- Android之使用HTTP协议访问网络
- Android使用HTTP协议访问网络
- Android使用Http协议访问网络
- Android使用Http协议访问网络
- Android入门-使用Http协议访问网络
- Android之使用HTTP协议访问网络
- Android—使用Http协议访问网络
- Android使用http协议访问网络
- Android Http协议访问网络
- Android - HTTP 协议访问网络
- Android:HTTP协议访问网络
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- 使用Http协议访问网络
- 文章标题
- 2017 多校第一场 HDU 6035 Colorful Tree 虚点,DFS,计算贡献
- 大数相加,相减,相乘算法(JAVA代码)
- Android蓝牙4.0 BLE开发坑总结
- c++callback回调实例
- android中使用http协议访问网络
- C++ 解析Json——jsoncpp vs2013 C++
- 7_24日C语言|整型提升相关介绍
- extgrid动态显示列
- OSG(OpenSceneGraphic) 渲染引擎架构--整体认识
- java读取xml文件的四种方法
- Apex学习:一个计划任务与批处理的Demo
- Nginx学习笔记(二) 负载均衡
- 空字符串+""等操作结果