HttpUrlConnection网络请求
来源:互联网 发布:2016淘宝小号购买平台 编辑:程序博客网 时间:2024/05/16 09:50
import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.webkit.WebView;import android.widget.Button;import android.widget.TextView;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class WebViewActivity extends AppCompatActivity { WebView webView; Button httpBtn; TextView sucess; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); webView = (WebView) findViewById(R.id.webView); //webView.loadUrl("http://www.baidu.com"); sucess = (TextView) findViewById(R.id.sucess); httpBtn = (Button) findViewById(R.id.button); httpBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { HttpUrlConnectionGet(); } }).start(); } }); } public void HttpUrlConnectionGet(){ HttpURLConnection uRLConnection = null; InputStream is; StringBuilder sb = new StringBuilder(); try { URL url = new URL("http://apis.baidu.com/txapi/naowan/naowan"); uRLConnection = (HttpURLConnection) url.openConnection(); uRLConnection.setConnectTimeout(5*1000); uRLConnection.setReadTimeout(5*1000); uRLConnection.setRequestProperty("apikey","7f52de784d67f3c53c1c1d6cfdb2e563"); uRLConnection.connect(); if (uRLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { is = uRLConnection.getInputStream(); byte[] bytes = new byte[1024]; int i = 0; while ((i = is.read(bytes)) != -1 ) { sb.append(new String(bytes,0,i,"utf-8")); } is.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (uRLConnection != null) { uRLConnection.disconnect(); } } Message message = handler.obtainMessage(1,sb.toString()); handler.sendMessage(message); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg != null && msg.what == 1) { String str = (String) msg.obj; webView.getSettings().setDefaultTextEncodingName("utf-8"); webView.getSettings().setJavaScriptEnabled(true); webView.loadDataWithBaseURL(null,str,"txet/html","utf-8",null); sucess.setText("请求成功"); } } };}
0 0
- 请求网络之HttpUrlConnection
- HttpURLConnection请求网络
- HttpURLConnection 网络请求数据
- HttpURLConnection+URl网络请求
- HttpURLConnection网络请求
- HttpURLConnection网络请求
- HttpUrlConnection网络请求
- HttpURLConnection请求网络
- HttpURLConnection实现网络请求
- URLConnection &HttpURLConnection 网络请求
- HttpURLConnection 请求网络
- HttpURLConnection、HttpClient网络请求
- HttpURLConnection网络请求
- HttpURLConnection---网络请求数据
- HttpUrlConnection 网络请求工具类
- HttpURLConnection请求网络小练习
- android HttpURLConnection网络请求数据
- HttpURLConnection网络请求案例3
- Hibernate:一对一双向外键关联
- Linux的vim文本编辑器
- 双系统 centos7 安装简记
- 记录一下python的数据结构 - set
- linux下升级(可逆)gcc4.8支持c++11
- HttpUrlConnection网络请求
- Hibernate搭建日志环境(log4j)及打印DDL语句
- Cgywin 环境变量 —— cygwin中运行命令提示command not found的解决方法
- 记录一下python的数据结构 - string
- 设计模式之单例模式
- 第十四天学习笔记
- 「MICCAI 2016」Reading Note
- 用java实现自定义随机数发生器
- gdb调试多文件程序