android网络实验
2019-05-22 09:45
16 查看
在MainActivity中 package com.example.administrator.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; 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 { TextView responseText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendRequest = (Button)findViewById(R.id.send_request); responseText = (TextView)findViewById(R.id.response_text); sendRequest.setOnClickListener(this); } public void onClick(View v){ if(v.getId() == R.id.send_request){ sendRequestWithHttpURLConnection(); } } private void sendRequestWithHttpURLConnection(){ new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; BufferedReader reader = null; try{ URL url = new URL("https://www.baidu.com"); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(80000); connection.setReadTimeout(80000); InputStream in = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null){ response.append(line); } showResponse(response.toString()); }catch (Exception e){ e.printStackTrace(); }finally { if(reader != null){ try{ reader.close(); }catch (IOException e){ e.printStackTrace(); } } if(connection != null){ connection.disconnect(); } } } }).start(); } private void showResponse(final String response){ runOnUiThread(new Runnable() { @Override public void run() { responseText.setText(response); } }); } }
在activity_main中
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.myapplication.MainActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/send_request" android:text="Send Request" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/response_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout> </android.support.constraint.ConstraintLayout>
相关文章推荐
- Android使用网络技术实现新闻客户端实验
- Android 实验——实现查看网络图片功能
- android Volley请求框架的简单使用——访问网络和加载图片
- 网络管理及实验(route命令和ip命令分别实现)
- 实验中的10Gbps无线网络可取代光纤
- Linux网络管理实验汇总
- Android之loadUrl访问网络资源与本地资源的方法
- Android网络通信的六种方式示例代码
- Android socket通信: 一问一答,一问多答客户端网络请求工具类的实现
- Android 获取是否已连接网络状态
- Android OkHttp网络请求
- Android网络连接处理
- Android 网络连接-重试机制-HttpRequestRetryHandler(HttpClient)
- iOS,Android网络抓包教程之tcpdump
- Android以请求参数来获取网络数据
- android 判断网络连接的工具类
- Android 网络请求方法
- Android开源项目推荐之「网络请求哪家强」
- Android 7.0 网络变化通知遇到的问题
- android--AsyncTask的应用(1)网络上图片的下载