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

wordpress 搬家 500网站的优化从哪里进行

wordpress 搬家 500,网站的优化从哪里进行,江苏省住房和城乡建设部网站,电工证免考拿证文章目录 WebView的用法使用http访问网络使用HttpURLConnection使用OkHttp 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。 点击跳转到网站。 WebView的用法 新建一个WebViewTest项目,然后修…

文章目录

      • WebView的用法
      • 使用http访问网络
        • 使用HttpURLConnection
        • 使用OkHttp

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。 点击跳转到网站。

WebView的用法

  新建一个WebViewTest项目,然后修改activity_main.xml中的代码。在布局中添加webView控件,用来显示网页。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><WebViewandroid:id="@+id/webView"android:layout_width="match_parent"android:layout_height="match_parent" />
</LinearLayout>

  然后修改MainActivity中的代码。

public class MainActivity extends AppCompatActivity {@SuppressLint("SetJavaScriptEnabled")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);WebView webView = (WebView) findViewById(R.id.webView);webView.getSettings().setJavaScriptEnabled(true);webView.setWebViewClient(new WebViewClient());webView.loadUrl("http://baidu.com");}
}

  getSettings()方法可以设置一些浏览器的属性。setJavaScriptEnabled()方法,让WebView支持JavaScript脚本。

  修改AndroidManifest.xml文件,并加入权限声明。

在这里插入图片描述

  代码可能会报 net::ERR_CLEARTEXT_NOT_PERMITTED 错误。

  可以创建文件:res/xml/network_security_config.xml。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><domain-config cleartextTrafficPermitted="true"><domain includeSubdomains="true">api.example.com(to be adjusted)</domain></domain-config>
</network-security-config>

  然后对AndroidManifest.xml文件做修改。

 <application...android:networkSecurityConfig="@xml/network_security_config"...>

使用http访问网络

使用HttpURLConnection

  首先需要获取HttpURLConnection的实例,一般只需创建一个URL对象,并传入目标的网络地址,然后调用一下openConnection()方法即可。

URL url = new URL(“http://www.baidu.com”);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

  HTTP请求常用的方法主要有两个:GET和POST。GET表示希望从服务器那里获取数据,而POST则表示希望提交数据给服务器。

connection.requestMethod = “GET”

  调用getInputStream()方法就可以获取到服务器返回的输入流。

InputStream in = connection.getInputStream();

  最后可以调用disconnect()方法将这个HTTP连接关闭。

connection.disconnect()

  新建一个NetworkTest项目,首先修改activity_main.xml中的代码。在不居中添加一个按钮用于发送HTTP请求,TextView用于将服务器返回的数据显示出来。借助ScrollView控件,以滚动的形式查看屏幕外的内容。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/sendRequestBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Send Request" /><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/responseText"android:layout_width="match_parent"android:layout_height="wrap_content" /></ScrollView>
</LinearLayout>

  接着修改MainActivity中的代码。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {TextView responseText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button sendRequest = (Button) findViewById(R.id.sendRequestBtn);responseText = (TextView) findViewById(R.id.responseText);sendRequest.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.sendRequestBtn) {sendRequestWithHttpURLConnection();}}private void sendRequestWithHttpURLConnection() {// 开启线程来发起网络请求new Thread(new Runnable() {@Overridepublic 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(8000);connection.setReadTimeout(8000);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() {@Overridepublic void run() {// 在这里进行UI操作,将结果显示到界面上responseText.setText(response);}});}
}

在这里插入图片描述

使用OkHttp

  OkHttp是一个开源项目,它不仅在接口封装上做得简单易用,就连在底层实现上也是自成一派,比起原生的HttpURLConnection,可以说是有过之而无不及,现在已经成了广大Android开发者首选的网络通信库。

  OkHttp的项目主页地址是:https://github.com/square/okhttp。

在使用OkHttp之前,我们需要先在项目中添加OkHttp库的依赖。编辑app/build.gradle文件。

dependencies {implementation(libs.appcompat)implementation(libs.material)implementation(libs.activity)implementation(libs.constraintlayout)testImplementation(libs.junit)androidTestImplementation(libs.ext.junit)androidTestImplementation(libs.espresso.core)implementation("com.squareup.okhttp3:okhttp:4.4.1")//okHttp
}

  添加上述依赖会自动下载两个库:一个是OkHttp库,一个是Okio库,后者是前者的通信基础。

  修改MainActivity中的代码。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {TextView responseText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button sendRequest = (Button) findViewById(R.id.sendRequestBtn);responseText = (TextView) findViewById(R.id.responseText);sendRequest.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.sendRequestBtn) {
//            sendRequestWithHttpURLConnection();sendRequestWithOkHttp();}}private void sendRequestWithOkHttp() {new Thread(new Runnable() {@Overridepublic void run() {try {OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url("https://www.baidu.com").build();Response response = client.newCall(request).execute();String responseData = response.body().string();showResponse(responseData);} catch (Exception e) {e.printStackTrace();}}}).start();}private void sendRequestWithHttpURLConnection() {// 开启线程来发起网络请求new Thread(new Runnable() {@Overridepublic 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(8000);connection.setReadTimeout(8000);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() {@Overridepublic void run() {// 在这里进行UI操作,将结果显示到界面上responseText.setText(response);}});}
}

在这里插入图片描述

http://www.hrbkazy.com/news/35336.html

相关文章:

  • 网站建设里怎么写文章产品推广方案怎么做
  • 网站营销活动页面制作百度推广登录入口下载
  • 阿里云wordpress有什么用成都百度推广排名优化
  • seo网络推广课程seo搜索引擎优化实训总结
  • 怎么做网站相关关键词重庆seo网络推广关键词
  • 免费做任务赚钱的网站有哪些新手怎么学网络运营
  • 兰州网站建设哪家公司好推广策略都有哪些
  • 惠东网站设计十大看免费行情的软件下载
  • 常州公司网站建设多少钱亚马逊关键词排名查询工具
  • 网站建站流程服装店营销策划方案
  • 如何做威客网站企业营销推广怎么做
  • 西安网站空间app推广全国代理加盟
  • 欧美做视频网站有哪些收录优美图片找不到了
  • 怎样建立个人网站?微信运营方案
  • 网站建设与管理需要什么软件有哪些内容百度账号申诉中心
  • c2c网站代表和网址2023新闻大事10条
  • 吉林省软环境建设网站营销型网页设计
  • 做网站管理系统搜索引擎都有哪些
  • 福田政府在线网站新网域名
  • 电商网站储值消费系统央视新闻
  • 网站备案完了怎么做昆明网站seo优化
  • ppt素材网站建设流程图网站制作公司怎么样
  • 制作网站的走马灯怎么做成人教育机构排行前十名
  • 海洋网站建设如何进行推广
  • 家电企业网站模板seo 推广怎么做
  • 珠海网站建设搜索引擎营销方案例子
  • 深圳龙华汽车网站建设西安自动seo
  • 做h5找图网站软件定制
  • 网站建设的重要性全网搜索指数查询
  • wordpress the_field长沙seo网站优化公司