httpurlconnection访问网络上的数据
来源:互联网 发布:重庆seo网站诊断 编辑:程序博客网 时间:2024/05/01 02:27
public class MainActivity extends Activity { @ViewInject(R.id.button_get) private Button getpButton; @ViewInject(R.id.button_post) private Button postbButton; private URL url; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewUtils.inject(MainActivity.this); try { url = new URL( "http://v.juhe.cn/toutiao/index?type=yule&key=c8fe066ae002d20891fbc48a1783a1ee"); } catch (MalformedURLException e) { e.printStackTrace(); } } @OnClick(value = { R.id.button_get, R.id.button_post }) public void myClick(View v) { switch (v.getId()) { //1.get请求 case R.id.button_get: new Thread() { public void run() { try { //通过url打开连接 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); //通对HttpURLConnection 对象进行连接 connection.connect(); BufferedReader br = new BufferedReader( new InputStreamReader( connection.getInputStream())); StringBuffer sBuffer = new StringBuffer(); String str = null; while ((str = br.readLine()) != null) { sBuffer.append(str); } } catch (IOException e) { e.printStackTrace(); } }; }.start(); Toast.makeText(MainActivity.this, "get请求成功", 0).show(); break;//2.post请求 case R.id.button_post: AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() { private ProgressDialog progressDialog; // 主线程(数据加载成功之前进行一些准备工作) protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("正在努力加载,请稍后......"); progressDialog.setTitle("提示信息"); progressDialog.show(); } // 主线程,当耗时任务完成是会调用此方法 protected void onPostExecute(String result) { super.onPostExecute(result); progressDialog.dismiss(); } // 子线程 protected String doInBackground(String... params) { try { url = new URL( "http://v.juhe.cn/toutiao/index?type=yule&key=c8fe066ae002d20891fbc48a1783a1ee"); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("POST"); connection.connect(); // 如果使用post请求,需要先将请求的参数发送给服务器 PrintWriter pWriter = new PrintWriter( connection.getOutputStream()); pWriter.print("type=yule&key=c8fe066ae002d20891fbc48a1783a1ee"); pWriter.close(); // 读取数据 BufferedReader bReader = new BufferedReader( new InputStreamReader( connection.getInputStream())); StringBuffer stringBuffer = new StringBuffer(); String str = null; while ((str = bReader.readLine()) != null) { stringBuffer.append(str); } Log.i("myTag", stringBuffer.toString()); } catch (IOException e) { e.printStackTrace(); } return null; } }; task.execute(); Toast.makeText(MainActivity.this, "post请求成功", 0).show(); break; } }
0 0
- httpurlconnection访问网络上的数据
- 基于HttpURLConnection的网络访问工具
- HttpURLConnection(访问网络)
- 使用HttpURLConnection访问网络
- HttpURLConnection访问网络
- 使用HttpURLConnection访问网络
- android HttpURLConnection网络访问
- 使用HttpURLConnection访问网络
- 用HttpURLConnection访问网络
- 网络访问之HttpURLConnection
- 使用HttpURLConnection访问网络
- 网络访问之HttpURLConnection
- 利用HttpClient or HttpURLConnection 借助tomcat实现在本地进行网络数据的访问
- 使用HttpURLConnection访问网络接口,展示列表数据
- HttpURLConnection:网络下载数据的工具类
- HttpURLConnection 网络请求数据
- HttpURLConnection解析网络数据
- HttpURLConnection---网络请求数据
- 冒泡排序
- NOIP2012国王游戏
- [QT]QT的安装和初体验
- 【文智背后的奥秘】系列篇——海量数据抓取
- 大家
- httpurlconnection访问网络上的数据
- Tomcat Https配置
- ORB_SLAM2安装与运行
- 原创
- web.xml文件的作用
- Ubuntu 16.04.1 Server 64bit 系统安装后的网络配置(Vmware Workstation 12.5 & Windows 10 X64 环境)
- c++初始化构造、初始化调用
- RabbitMQ进程结构分析与性能调优
- 【xunSearch】C++ 搜索引擎xunSearch的配置、安装