在android用Get方式发送http请求
烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...。。。
一、在android用Get方式发送http请求,使用的是java标准类,也比较简单。
主要分以下几步:
1.构造URL
URL url = new URL(String path);
2.设置连接
httpURLConnection = (HttpURLConnection) url.openConnection();
//超时时间
httpURLConnection.setConnectTimeout(3000);
//表示设置本次http请求使用GET方式
httpURLConnection.setRequestMethod("GET");
int responsecode = httpURLConnection.getResponseCode();//返回至为响应编号,如:HTTP_OK表示连接成功。
3.获取返回数据
if(responsecode == HttpURLConnection.HTTP_OK){
inputStream = httpURLConnection.getInputStream();
} //得到inputStream 就好办啦。
new InputStreamReader(inputStream,"utf-8")
4.关闭连接
void disconnect()
二、下面通过一个简单的Demo实现get方式的请求:
package com.http.get; import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class HttpUtils { private static String URL_PATH="http://www.baidu.com";
private static HttpURLConnection httpURLConnection = null;
public HttpUtils(){ } public static void shuchu(){
InputStream inputStream = getInputStream();
String result;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
result = "";
String line = "";
try {
while((line = reader.readLine())!= null){
result = result+ line;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
httpURLConnection.disconnect();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
/**
* 获取服务端的数据,以InputStream返回
* @return
*/
public static InputStream getInputStream(){
InputStream inputStream = null; try {
URL url = new URL(URL_PATH);
if(url != null){
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
//超时时间
httpURLConnection.setConnectTimeout(3000);
//表示设置本次http请求使用GET方式
httpURLConnection.setRequestMethod("GET");
int responsecode = httpURLConnection.getResponseCode(); if(responsecode == HttpURLConnection.HTTP_OK){
inputStream = httpURLConnection.getInputStream();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
public static void main(String[] args){
//保存文件到本地
//saveImageToDisk();
shuchu();
}
}
//因为get方式是这ava标准类,直接写的java程序,不过都一样,android中也是一样的。。
简单的访问了百度,返回的就是百度搜索首页的源代码:图片一直上传不了。。。就不截图啦。
在android用Get方式发送http请求的更多相关文章
- android中Post方式发送HTTP请求
Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...
- 安卓基础之Get方式发送http请求
本文参考作者:超超boy 链接:https://www.cnblogs.com/jycboy/p/post01.html 一.在android用Get方式发送http请求,使用的是java标准类. 主 ...
- WebClient以POST方式发送Web请求
本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求 Cookies前后一致.其中application/x-www ...
- AJAX方式发送远程请求报错:No 'Access-Control-Allow-Origin' header
AJAX GET方式发送远程请求,chrome开发者工具console中报错:XMLHttpRequest cannot load http://www.shikezhi.com/ajax/getDa ...
- python通过get,post方式发送http请求和接收http响应的方法,pythonget
python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...
- 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...
- Android 的OkHttp(发送网络请求)
今天讲的是和HttpURLConnection差不多的OkHttp; 先把网站献上: 官网介绍okhttp的: https://square.github.io/okhttp/ 下载postman的: ...
- Android面向HTTP协议发送post请求
/** * 採用post请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pu ...
随机推荐
- 原生js编写的安全色拾色器
<html > <head> <meta http-equiv="Content-Type" content="text/html; cha ...
- Elasticsearch增删改查 之 —— Get查询
GET API是Elasticsearch中常用的操作,一般用于验证文档是否存在:或者执行CURD中的文档查询.与检索不同的是,GET查询是实时查询,可以实时查询到索引结果.而检索则是需要经过处理,一 ...
- SQL Server代理(3/12):代理警报和操作员
SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 如我们在这个系列的文章里所见,SQL Se ...
- 用Latex写学术论文:作者(Author)&摘要(Abstract)
标题&作者 1.标题 \title{} "Line breaks (\\) may be used to equalize the length of the title lines ...
- 五、BLE(下)
1.1 GATT server Service 通过走读代码, GATT Server作为一个GATT service,我是没有发现其发挥了多大功能,其负责处理的消息GATT_SERVER ...
- Python语言特性之2:元类
问题:Python中的元类(metaclasses)是什么?一般使用它干什么? 原地址:http://stackoverflow.com/questions/100003/what-is-a-meta ...
- C#的变迁史 - C# 5.0 之调用信息增强篇
Caller Information CallerInformation是一个简单的新特性,包括三个新引入的Attribute,使用它们可以用来获取方法调用者的信息, 这三个Attribute在Sys ...
- TabHost的使用
Android中的选项卡是用TabHost实现的. 首先,定义TabHost的布局文件: <?xml version="1.0" encoding="utf-8&q ...
- android 密码输入通过复选框实现明文密文显示
editText1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//将文本框的内容以明文显示 edit ...
- 【算法和数据结构】_12_小算法_abs_fabsf_fabsd_大端小端判断
最近学习要用到求绝对值函数,看了一下有很多种的abs函数,因此想自己实现这些代码. 下面是我进行测试的代码: #include <stdio.h> typedef unsigned int ...