当前位置: 首页 > news >正文

东莞公司网站制作/个人网站建设

东莞公司网站制作,个人网站建设,设计公司团队,学校网页设计说明Android 上发送 HTTP 请求的一般有两种方式,HttpURLConnection 和 HttpClient。下面分别简述两种方式的用法。 1. HttpURLConnection 1, 获取 HttpURLConnection 的实例。 一般只需 new 出一个 URL 对象,并传入目标的网络地址,然后调用 op…

Android 上发送 HTTP 请求的一般有两种方式,HttpURLConnection 和 HttpClient。下面分别简述两种方式的用法。

1. HttpURLConnection

1, 获取 HttpURLConnection 的实例。
一般只需 new 出一个 URL 对象,并传入目标的网络地址,然后调用 openConnection()方法即可。如下所示:

URL url = new URL("http://www.baidu.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

2, 设置 HTTP 请求方法。
常用的方法有两个:GET 和 POST。GET 表示希望从服务器那里获取数据,而 POST 则表示希望提交数据给服务器。写法如下:

connection.setRequestMethod("GET");

3, 其他设置,如设置连接超时、读取超时的毫秒数等。可根据实际情况来写,例如:

connection.setConnectTimeout(5000);// 设置连接超时为5000毫秒
connection.setReadTimeout(5000);

4, 用 getInputStream() 方法获取服务器返回的输入流,然后读取输入流。代码如下所示:

InputStream in = connection.getInputStream();

5, 关闭 HTTP 连接

connection.disconnect();

MainActivity 代码实现:

public class MainActivity extends Activity implements OnClickListener {public static final int SHOW_RESPONSE = 0;private Button sendRequest;private TextView responseText;private Handler handler = new Handler() {public void handleMessage(Message msg) {switch(msg.what) {case SHOW_RESPONSE:String response = (String) msg.obj;// 在这里进行UI操作,将结果显示到界面上responseText.setText(response);}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendRequest = (Button) findViewById(R.id.send_request);responseText = (TextView) findViewById(R.id.response);sendRequest.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v.getId() == R.id.send_request) {sendRequestWithHttpURLConnection();}}private void sendRequestWithHttpURLConnection() {// 开启线程来发送网络请求new Thread(new Runnable() {@Overridepublic void run() {HttpURLConnection connection = null;try {// 获取 HttpURLConnection 的实例URL url = new URL("http://www.baidu.com");connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");// 设置 HTTP 请求方法为 GETconnection.setConnectTimeout(5000);// 设置连接超时、读取超时的毫秒数connection.setReadTimeout(5000);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 message = new Message();message.what = SHOW_RESPONSE;// 将服务器返回的结果存放到 Message 中message.obj = response.toString();handler.sendMessage(message);} catch(Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();// 关闭 HTTP 连接}}                               }}).start();     }
}

声明网络权限:

<uses-permission android:name="android.permission.INTERNET" />

运行后点击按钮,效果如下图所示:
HTTP_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>

此外,若要提交数据给服务器,只需把 HTTP 请求方法改为 POST,并在获取输入流之前把要提交的数据写出即可。
需要注意的是:每条数据都要以 键 - 值 对的形式存在,数据与数据之间用 & 符号隔开。例如向服务器提交用户名和密码,可以这样写:

connection.setRequestMethod("POST");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes("username=hello&password=123");

2. HttpClient

HttpClient 可以完成和 HttpURLConnection 几乎一样的效果。但需要注意的是,HttpClient 是一个接口,因此无法创建它的实例。

1, 通常会创建一个 DefaultHttpClient 的实例,

HttpClient httpClient = new DefaultHttpClient();

2, 若想要发起一条 GET 请求,就可以创建一个 HttpGet 对象,并传入目标的网络地址,然后调用 HttpClient 的 execute()方法即可:

HttpGet httpGet = new HttpGet("http://www.baidu.com");
HttpResponse httpResponse = httpClient.execute(httpGet);

若是发起一条 POST 请求会比 GET 稍微复杂,我们需要创建一个 HttpPost 对象,并传入目标的网络地址,如下所示:

HttpPost httpPost = new HttpPost("http://www.baidu.com");

然后通过一个 NameValuePair 集合来存放待提交的参数,并将这个参数集合传入到一个UrlEncodedFormEntity 中,然后调用 HttpPost 的 setEntity()方法将构建好的 UrlEncodedFormEntity 传入,如下所示:

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "admin"));
params.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
httpPost.setEntity(entity);

接下来的操作就和 HttpGet 一样了,调用 HttpClient 的 execute()方法,并将 HttpPost 对象传入即可:
httpClient.execute(httpPost);

执行 execute()方法后会返回一个 HttpResponse 对象,服务器返回的所有信息就会包含在这里。通常我们会先取出服务器返回的状态码,如果等于 200 就说明请求和响应都成功了,如下所示:

if (httpResponse.getStatusLine().getStatusCode() == 200) {// 请求和响应都成功了
}

3, 在这个 if 判断的内部取出服务返回的内容。可调用 getEntity()方法获取到一个 HttpEntity 实例,然后再用 EntityUtils.toString()这个静态方法将 HttpEntity 转换成字符串即可:

HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity);

注意:
如果服务器返回的数据是带有中文的,直接调用 EntityUtils.toString()方法进行转换会有乱码的情况出现,这个时候只需要在转换的时候将字符集指定成 utf-8 就可以了,如下所示:

String response = EntityUtils.toString(entity, "utf-8");

MainActivity 代码实现:

public class MainActivity extends Activity implements OnClickListener {public static final int SHOW_RESPONSE = 0;private Button sendRequest;private TextView responseText;private Handler handler = new Handler() {public void handleMessage(Message msg) {switch(msg.what) {case SHOW_RESPONSE:String response = (String) msg.obj;// 在这里进行UI操作,将结果显示到界面上responseText.setText(response);}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendRequest = (Button) findViewById(R.id.send_request);responseText = (TextView) findViewById(R.id.response);sendRequest.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v.getId() == R.id.send_request) {sendRequestWithHttpClient();}}private void sendRequestWithHttpClient() {new Thread(new Runnable() {@Overridepublic void run() {try {HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet("http://www.baidu.com");HttpResponse httpResponse = httpClient.execute(httpGet);if (httpResponse.getStatusLine().getStatusCode() == 200) {// 请求和响应都成功了HttpEntity entity = httpResponse.getEntity();String response = EntityUtils.toString(entity, "utf-8");Message message = new Message();message.what = SHOW_RESPONSE;// 将服务器返回的结果存放到 Message 中message.obj = response.toString();handler.sendMessage(message);}} catch (Exception e) {e.printStackTrace();}}}).start();     }
}

运行后点击按钮,效果如下图所示:
HTTP_2

PS:二者看起来很像,不过个人觉得有些小差别。

3. 常用 http 请求写法

发送 http 请求的代码基本都是相同的,没必要每次发送的时候都写一遍。一般将这些通用的网络操作提取到一个公共类中,并提供一个静态方法,需要发起网络请求时调用该方法即可。常用写法如下:

首先定义一个接口:

public interface HttpCallbackListenr {/*** 服务器成功响应请求的时候调用*/void onFinish(String response);/*** 进行网络操作出现错误的时候调用*/void onError(Exception e);
}

定义一个类,类中定义一个静态方法:

public class HttpUtil {public static void sendHttpRequest(final String address, final HttpCallbackListenr listener) {new Thread(new Runnable() {@Overridepublic void run() {HttpURLConnection connection = null;try {URL url = new URL(address);connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);// 设置连接超时、读取超时的毫秒数connection.setReadTimeout(5000);connection.setDoInput(true);// 以后可以使用conn.getInputStream().read();connection.setDoOutput(true);// 以后可以使用conn.getOutputStream().write()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);}if (listener != null) {listener.onFinish(response.toString());}} catch (Exception e) {if (listener != null) {listener.onError(e);}} finally {if (connection != null) {connection.disconnect();// 关闭 HTTP 连接}}}}).start();}}

网络请求属于耗时操作,

  • 若 sendHttpRequest() 方法内部不开线程,有可能导致调用 sendHttpRequest() 方法的时候主线程被阻塞;
  • 若开启子线程,那么服务器响应的数据是无法进行返回的,所有的耗时逻辑都是在子线程里进行的, sendHttpRequest()方法会在服务器还来得及响应的时候就执行结束了,当然也无法返回响应的数据。【暂时还不理解,先记下来】
  • 因此用到了 Java 的回调机制,即定义一个回调接口。
http://www.lbrq.cn/news/1241245.html

相关文章:

  • 深圳哪家做网站好/社群营销的具体方法
  • 长宁区网站设计建设/推广方案策划
  • 上海的广告公司网站建设/长沙做引流推广的公司
  • 顺德高端网站/西安百度关键词优化
  • 杭州企业网站建设 哪里好/seo搜索优化
  • 东莞深圳网站建设/seo l
  • 电白网站建设公司/网络营销推广的基本手段
  • 网站制作 西安/民生热点新闻
  • 简述网站建设基本流程/如何推广品牌
  • phpcms v9网站建设入门/重庆网站排名提升
  • r语言做网站/关键词seo排名怎么选
  • 在线阅读网站开发教程/百度官方网平台
  • 香港空间做网站速度慢的解决方法/海外网络推广服务
  • 西城 网站公安备案/厦门seo推广外包
  • 信阳高端网站建设/网络营销师证书怎么考
  • 怎么个人网站设计/企业宣传标语
  • 电商网站怎么推广/网站客服系统
  • 网站维护好的方法/域名注册网站
  • 为什么网站不建议做充值功能/原版百度
  • 广东做网站的公司/百度竞价推广开户费用
  • php网站 怎么取得后台管理权限/郑州seo培训班
  • 网站建设的基本流程规范/今日关注
  • 郑州做的比较好网站公司吗/如何做百度竞价推广
  • 网站为什么做优化ppt/b站网站推广
  • 网站建设推荐中企动力/手机百度账号登录个人中心
  • wordpress 模板分页/广州优化疫情防控举措
  • seo人才/成都seo正规优化
  • 怎么做好网站/网站关键词优化建议
  • 网站备案周期/windows优化大师会员
  • 推荐家居企业网站建设/怎么下载有风险的软件
  • Codeforces Round 1039 (Div. 2) A-C
  • uniapp 数组的用法
  • Piriority_queue
  • 8.1IO进程线程——文件IO函数
  • Vue项目使用ssh2-sftp-client实现打包自动上传到服务器(完整教程)
  • 【刷题】东方博宜oj 1412-快速幂(零基础,简单易懂)