当前位置: 首页 > news >正文

日本做a的小视频在线观看网站宁德市古田县

日本做a的小视频在线观看网站,宁德市古田县,怎样能有个人网站,重庆工程造价信息价查询网络操作 Android平台下的原生网络操作可以分为以下几步: 创建URL对象;通过URL对象获取HttpURLConnection对象;通过HttpURLConnection对象设置请求头键值对、网络连接超时时间等;通过HttpURLConnection对象的connect()方法建立网…

网络操作

Android平台下的原生网络操作可以分为以下几步:

  1. 创建URL对象;
  2. 通过URL对象获取HttpURLConnection对象;
  3. 通过HttpURLConnection对象设置请求头键值对、网络连接超时时间等;
  4. 通过HttpURLConnection对象的connect()方法建立网络连接;
  5. 通过响应码判断请求是否成功,如果成功的话获取输入流,对返回的结果进行处理并显示;
    这里要注意的是,网络请求操作不能直接放在ui线程中,需要另外开启一个线程去处理网络请求,因为网络请求操作是一个耗时的操作。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><Buttonandroid:id="@+id/btn_sendget"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击发送get请求"/><Buttonandroid:id="@+id/btn_sendpost"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击发送post请求"/><TextViewandroid:id="@+id/tv"android:layout_width="match_parent"android:layout_height="wrap_content"android:scrollbars="vertical"android:fadeScrollbars="false"/></LinearLayout>

GET请求

这里说一下用到的方法的作用:
setConnectTimeout:设置请求超时时间;
setRequestMethod:设置请求方法;
setRequestProperty:设置请求属性;
connect():建立网络连接;
getResponseCode():获取响应码;

 try {new Thread(new Runnable() {@Overridepublic void run() {try {URL url = new URL("http://www.imooc.com/api/teacher?type=2&page=1");HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setConnectTimeout(30*1000);connection.setRequestMethod("GET");connection.setRequestProperty("Content-Type","application/json");connection.setRequestProperty("Charset","UTF-8");connection.setRequestProperty("Accept-Charset","UTF-8");connection.connect();int responseCode = connection.getResponseCode();//如果请求成功if (responseCode == HttpURLConnection.HTTP_OK) {//获取输入流InputStream inputStream = connection.getInputStream();byte[] bytes = new byte[1024];StringBuffer result = new StringBuffer();int len = 0;while ((len = inputStream.read(bytes)) != -1) {result.append(new String(bytes,0,len));}//不能直接在其它线程更新ui线程,可以使用此方法或者使用handlerrunOnUiThread(new Runnable() {@Overridepublic void run() {tv.setText(result);}});Log.d(TAG, "onClick: " + result);}}catch (Exception e) {e.printStackTrace();}}}).start();} catch (Exception e) {e.printStackTrace();}

POST请求

 new Thread(new Runnable() {@Overridepublic void run() {try {URL url = new URL("http://www.imooc.com/api/teacher?type=2&page=1");HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(30 * 1000);connection.setRequestProperty("Content-Type","application/json");connection.setRequestProperty("Charset","UTF-8");connection.setRequestProperty("Accept-Charset","UTF-8");connection.connect();int responseCode = connection.getResponseCode();if (responseCode == HttpURLConnection.HTTP_OK) {InputStream inputStream = connection.getInputStream();int  len = 0;byte[] bytes = new byte[1024];StringBuffer result = new StringBuffer();while ((len = inputStream.read(bytes)) != -1) {result.append(new String(bytes,0,len));}Log.d(TAG, "responseCode: " + responseCode);Log.d(TAG, "result: " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {tv.setText(result);}});} else {Log.d(TAG, "responseCode: " + responseCode);}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}).start();

解析返回的json数据

 public List<Lesson> JsonToArray(String str) {List<Lesson> lessonList = new ArrayList<>();try {JSONObject jsonObject = new JSONObject(str);JSONArray jsonArray = jsonObject.getJSONArray("data");for (int i = 0; i < jsonArray.length(); i++) {JSONObject lesson = (JSONObject) jsonArray.get(i);int id = lesson.getInt("id");int learner = lesson.getInt("learner");String name = lesson.getString("name");String picSmall = lesson.getString("picSmall");String picBig = lesson.getString("picBig");String description = lesson.getString("description");Lesson lesson1 = new Lesson();lesson1.setId(id);lesson1.setDescription(description);lesson1.setLearner(learner);lesson1.setPicBig(picBig);lesson1.setPicSmall(picSmall);lesson1.setName(name);lessonList.add(lesson1);}} catch (JSONException e) {e.printStackTrace();}return lessonList;}

注意事项

Android9.0以后,使用http访问网络需要添加配置文件;首先在res目录下新建一个xml资源目录,然后创建配置文件,配置文件的名称必须是neteork_security_config.xml;
内容:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><!--允许http请求的加载--><base-config cleartextTrafficPermitted = "true"/>
</network-security-config>
http://www.hkea.cn/news/375633/

相关文章:

  • 自建网站网址雅虎搜索引擎首页
  • 注册科技有限公司可以做网站吗百度搜索排名机制
  • 武汉做网站好网站制作多少钱一个
  • 安阳网站建设怎么从网上找客户
  • 文章博客媒体网站模板怎样在百度上打广告
  • 做网站是不是要模板直接打开百度
  • 哪个网站做app推广服务商
  • 中国哪里在大建设网站优化培训学校
  • 自己做的网站点首页出错腾讯广告代理商加盟
  • 如何做免费的网站推广东莞百度seo
  • 宜昌网站制作公司百度竞价官网
  • 建站公司网站模板论坛怎么建网站
  • 上海做b2b网站公司深圳公司网络推广该怎么做
  • 自己做的网站怎么在百度可以查到网络小说网站三巨头
  • 怎么做网站客服弹窗站长之家seo工具包
  • 自己建一个电商网站吗网络营销的定义
  • 专门做金融的招聘网站四川seo选哪家
  • wordpress nginx伪静态配置拼多多seo怎么优化
  • 深圳网站开发电话惠州网络营销
  • 中宁网站建设公司商城全网推广运营公司
  • 网站文章列表如何排版郑州seo技术培训班
  • 小型b2c网站百度开户渠道商哪里找
  • 武进区住房和城乡建设局网站爱站网能不能挖掘关键词
  • APP手机端电子商务网站建设营销成功的案例
  • 公司网站引导页百度搜索关键词排名优化技术
  • 网站开发与维护学什么网站建设seo优化培训
  • 常州网站开发百度网盘电脑版官网
  • wordpress安全权限关键词优化公司哪家好
  • 银川做网站服务google play下载安卓
  • 科技型中小企业服务网安徽搜索引擎优化seo