关于HttpURLConnection的用法
发布日期:2021-06-29 04:56:02
浏览次数:4
分类:技术文章
本文共 5087 字,大约阅读时间需要 16 分钟。
package com.example.fafa.mydemoaboutnet;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mBtn; private TextView mViewText; private Button mSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iniView(); } private void iniView() { mBtn = (Button) findViewById(R.id.my_btn); mViewText = (TextView) findViewById(R.id.view_text); mSend = (Button) findViewById(R.id.btn_send); mSend.setOnClickListener(this); mBtn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.my_btn: getMessages();//服务器端获取数据 break; case R.id.btn_send: sendMessages();//客户端发送数据比如用户名密码之类的 break; } } private void sendMessages() { new Thread(new Runnable() { HttpURLConnection connection=null; BufferedReader mreader=null;//这一步写在外面不然finally无法处理 @Override public void run() { try { URL url= new URL("http://192.168.191.1:8080/bigweb/tologin"); connection= ((HttpURLConnection) url.openConnection()); connection.setRequestMethod("POST"); DataOutputStream out =new DataOutputStream(connection.getOutputStream()); out.writeBytes("username=yang&password=123456"); //post请求 在这里我是用的朋友的阿帕奇上的一个登陆的模块 判断我传递过去的数据 然后返回给我一个字符串 //结果接受成功 connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); String line; mreader=new BufferedReader(new InputStreamReader(in)); StringBuilder response= new StringBuilder(); while((line=mreader.readLine())!=null){ response.append(line); } showView(response.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if(mreader==null){ try { mreader.close();//你请求的网址如果不正确 这里会报错 } catch (IOException e) { e.printStackTrace(); } } if (connection!=null){ connection.disconnect(); } } } }).start(); } private void getMessages() { new Thread(new Runnable() { HttpURLConnection connection=null; BufferedReader mreader=null;//这一步写在外面不然finally无法处理 @Override public void run() { try { URL url= new URL("https://www.baidu.com"); connection= ((HttpURLConnection) url.openConnection());//获取实例传入目标地址 connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); mreader=new BufferedReader(new InputStreamReader(in)); String line; StringBuilder response= new StringBuilder(); while((line=mreader.readLine())!=null){ response.append(line); } showView(response.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if(mreader==null){ try { mreader.close();//你请求的网址如果不正确 这里会报错 } catch (IOException e) { e.printStackTrace(); } } if (connection!=null){ connection.disconnect(); } } } }).start(); } private void showView(final String s) { runOnUiThread(new Runnable() { @Override public void run() { //这里进行了 UI操作子线程是不能更新UI的 需要用handler 但是这里的runOnUiRhread是可以的 //因为它本身就是进行了handler的 点进去你会发现他进行的是一个senddelaymessage操作 mViewText.setText(s); } }); }}
以上是代码 XML就不贴出来了 XML就是二个按钮 一个是发送数据 一个是接受数据 还有一个ScrollView
里面存放一个TextView用来显示服务器端返回的数据
转载地址:https://blog.csdn.net/zhang_linhua/article/details/73739868 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月19日 15时57分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
### how to write file faster
2019-04-29
未来几年,谁能拯救手机拍照功能?
2019-04-29
安装与配置Flutter开发环境
2019-04-29
读刘润的《新零售》
2019-04-29
5G谈“风暴”可能为之尚早,芯片厂商之间的拉锯战才是这场变革的热身赛 ...
2019-04-29
算法笔记之回溯法(1)
2019-04-29
大家都知道Python库很强大,却不知道还有强大工具包 poetry? ...
2019-04-29
《TCP IP 详解卷1:协议》阅读笔记 - 第十六章
2019-04-29
编程在哪学,可以免费自学编程的10个网站
2019-04-29
蓝桥杯2013年JavaB组
2019-04-29
Lambda简单示例
2019-04-29
BootStrap4登录表单验证示例
2019-04-29
划重点—2020年CCSP的考试大纲有这些变化
2019-04-29
测一测|企业信息安全管理10道小题你能答对几个?
2019-04-29
信息安全意识主题分享-数据安全
2019-04-29
请收下这份CCSP云安全专家认证备考攻略(合集)
2019-04-29
一文带你了解PDPF隐私与数据保护认证
2019-04-29
WireShark网络安全分析
2019-04-29
【备考干货】一次拿下CISM认证的经验分享
2019-04-29