@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView) findViewById(R.id.textView1);
String httpUri = "http://www.baidu.com";
String result = "";
URL url = null;
try {
url = new URL(httpUri);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (url != null) {
try {
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(10000);// 10s内连不上就断开
// 因为这个是post请求,设立需要设置为true
// urlConn.setDoOutput(true);
// urlConn.setDoInput(true);
// 设置以POST方式
// urlConn.setRequestMethod("POST");
// Post 请求不能使用缓存
// urlConn.setUseCaches(false);
// urlConn.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// urlConn.setRequestProperty("Content-Type",
// "application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
urlConn.connect();
// DataOutputStream流
OutputStream outputStream = urlConn.getOutputStream();// 向服务器写入
DataOutputStream out = new DataOutputStream(outputStream);
// DataOutputStream is objectOutputStream的子类,也可以用objectOutputStream类
// 要上传的参数
// String content = "name=gyf";
// 将要上传的内容写入流中
out.writeBytes(new String("name=gyf"));// 要是用objectOutputStream就是out.writeObject(content);//写入服务器的参数,但是放到内存中了
// content = "&wife=" + URLEncoder.encode("lyx", "gb2312");
// out.writeBytes(content);
// 刷新、关闭
out.flush();// 真正的写过去了
out.close();
// 获服务器取数据
InputStream inputStream = urlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);// 读字符串用的。
String inputLine = null;
// 使用循环来读取获得的数据,把数据都村到result中了
while (((inputLine = reader.readLine()) != null)) {
// 我们在每一行后面加上一个"\n"来换行
result += inputLine + "\n";
}
reader.close();// 关闭输入流
// 关闭http连接
urlConn.disconnect();
// 设置显示取得的内容
if (result != null) {
textView.setText(result);
}
else {
textView.setText("读取的内容为NULL");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
分享到:
相关推荐
android使用Java原生httpUrlConnection进行get请求
Android使用HttpURLConnection访问网络
b=(Button)this.findViewById(R.id.b);//图片浏览按钮 et=(EditText)this....//取得返回的InputStream bitmap = BitmapFactory.decodeStream(is);//将InputStream变成Bitmap is.close();//关闭InputStream
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
Android HttpUrlConnection使用方法 post and get json.
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
java HttpURLConnection示例 HttpURLConnection工具
在 Android 中对 HttpURLConnection 的简单使用,仅供参考。
android之通过HttpURLConnection类查看网络图片、网络源码的例子android之通过HttpURLConnection类查看网络图片、网络源码的例子
NULL 博文链接:https://lydia-fly.iteye.com/blog/2033391
读取返回的输入流中的数据,并将其中的数据转换为byte数组 使用InputStream 的read方法以及ByteArrayOutputStream的wirte方法 inputStream.read(buffer) outputStream.write(buffer, 0, len) outputStream....
本人原创测试了百度,搜狗,360搜索都可以获取重定向后的真实地址,直接运行就可以,也可以根据自己项目具体修改功能。java使用HttpURLConnection获取百度搜狗360搜索链接重定向真实目标链接地址url。
用于对基于HTTP的服务进行抓取 HttpClient 现在是HttpComponents 把问题搞复杂了 其实Java内置的HttpURLConnection很好的 内含文档 使用方法见http: blog csdn net jdgdf566 article details 16357677
Httpurlconnection请求网络数据(文本&图片),使用Android Studio
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下。 正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1。一直想不明白,同一个程序我调用...
针对httpURLConnection 的简单封装,简单易用,支持上传下载 用法自已研究吧、
使用HttpURLConnection实现断点续传
CUrl类是以命令行工具CUrl为参考,使用标准Java的HttpURLConnection实现的Http工具类
本例子是 java 的 HttpURLConnection 使用方法,比较简单,但是涵盖了post和get两种方法,而且处理了乱码。