国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

p2p網(wǎng)站如何做測(cè)試工具目前疫情最新情況

p2p網(wǎng)站如何做測(cè)試工具,目前疫情最新情況,網(wǎng)站域名可以做端口映射嗎,免費(fèi)網(wǎng)站制作平臺(tái)下載實(shí)驗(yàn)六:Android 的網(wǎng)絡(luò)編程基礎(chǔ) 6.1 實(shí)驗(yàn)?zāi)康? 本次實(shí)驗(yàn)的目的是讓大家熟悉 Android 開(kāi)發(fā)中的如何獲取天氣預(yù)報(bào),包括了 解和熟悉 WebView、WebService 使用、網(wǎng)絡(luò)編程事件處理等內(nèi)容。 6.2 實(shí)驗(yàn)要求 熟悉和掌握 WebView 使用 了解 Android 的網(wǎng)絡(luò)編程…

實(shí)驗(yàn)六:Android 的網(wǎng)絡(luò)編程基礎(chǔ)

6.1 實(shí)驗(yàn)?zāi)康?/h3>

本次實(shí)驗(yàn)的目的是讓大家熟悉 Android 開(kāi)發(fā)中的如何獲取天氣預(yù)報(bào),包括了

解和熟悉 WebView、WebService 使用、網(wǎng)絡(luò)編程事件處理等內(nèi)容。

6.2 實(shí)驗(yàn)要求

  • 熟悉和掌握 WebView 使用

  • 了解 Android 的網(wǎng)絡(luò)編程

  • 熟悉和掌握 WebService 使用

6.3 實(shí)驗(yàn)內(nèi)容

【練習(xí) 6.1】基于 Webview 的獲取天氣預(yù)報(bào)

1. 項(xiàng)目結(jié)構(gòu)

項(xiàng)目名:WebViewWeather

項(xiàng)目結(jié)構(gòu):

  • res/layout/activity_main.xml:主布局文件
  • res/values/strings.xml:字符串資源文件
  • src/com/example/webview/MainActivity.java:主Activity文件
  • AndroidManifest.xml:Android清單文件
2. 主布局文件 (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:gravity="center_horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 按鈕布局 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!-- 按鈕:北京 --><Buttonandroid:id="@+id/bj"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/bj"android:textSize="30dp" /><!-- 按鈕:上海 --><Buttonandroid:id="@+id/sh"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/sh"android:textSize="30dp" /><!-- 按鈕:哈爾濱 --><Buttonandroid:id="@+id/heb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/heb"android:textSize="30dp" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!-- 按鈕:廣州 --><Buttonandroid:id="@+id/gz"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/gz"android:textSize="30dp" /><!-- 按鈕:長(zhǎng)春 --><Buttonandroid:id="@+id/cc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/cc"android:textSize="30dp" /><!-- 按鈕:沈陽(yáng) --><Buttonandroid:id="@+id/sy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/sy"android:textSize="30dp"android:layout_gravity="right" /></LinearLayout><!-- WebView組件 --><WebViewandroid:id="@+id/webView1"android:layout_width="match_parent"android:layout_height="0dip"android:focusable="false"android:layout_weight="1"/>
</LinearLayout>
3. 字符串資源文件 (strings.xml)
<resources><string name="app_name">WebViewWeather</string><string name="go">GO</string><string name="bj">北京</string><string name="sh">上海</string><string name="gz">廣州</string><string name="heb">哈爾濱</string><string name="cc">長(zhǎng)春</string><string name="sy">沈陽(yáng)</string>
</resources>
4. 主Activity文件 (MainActivity.java)
package com.example.webviewweather;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private WebView webView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);webView = (WebView) findViewById(R.id.webView1);webView.getSettings().setJavaScriptEnabled(true);webView.setWebChromeClient(new WebChromeClient());webView.setWebViewClient(new WebViewClient());webView.loadUrl("http://m.weather.com.cn/mweather/");webView.setInitialScale(57  4);Button bj = (Button) findViewById(R.id.bj);bj.setOnClickListener(this);Button sh = (Button) findViewById(R.id.sh);sh.setOnClickListener(this);Button heb = (Button) findViewById(R.id.heb);heb.setOnClickListener(this);Button cc = (Button) findViewById(R.id.cc);cc.setOnClickListener(this);Button sy = (Button) findViewById(R.id.sy);sy.setOnClickListener(this);Button gz = (Button) findViewById(R.id.gz);gz.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.bj:openUrl("101010100");break;case R.id.sh:openUrl("101020100");break;case R.id.heb:openUrl("101050101");break;case R.id.cc:openUrl("101060101");break;case R.id.sy:openUrl("101070101");break;case R.id.gz:openUrl("101280101");break;}}private void openUrl(String id) {webView.loadUrl("http://m.weather.com.cn/mweather/" + id + ".shtml");}
}
5. Android清單文件 (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.webview" ><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme" ><activity android:name=".MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
</manifest>
6.運(yùn)行效果

image-20231116232704967


【練習(xí) 6.2】基于 WebService 的手機(jī)歸屬地查詢

1. 添加 ksoap2-android 庫(kù)
  1. 在 ksoap2-android 的項(xiàng)目下載網(wǎng)站 下載 ksoap2-android-assembly-2.4-jar-with-dependencies.jar。

    • 如果難以下載,可以在隨書(shū)光盤(pán)中找到該 JAR 包。
  2. 將下載的 ksoap2-android JAR 包添加到工程的 lib 目錄下。

  3. 右鍵點(diǎn)擊 JAR 包,選擇 “Add as library”,將 ksoap2-android 集成到 Android 項(xiàng)目中。

2. WebService 配置
  1. 打開(kāi) http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx。

  2. 點(diǎn)擊 “getMobileCodeInfo” 進(jìn)入說(shuō)明頁(yè),獲取以下關(guān)鍵信息:

    • 作用域 TargetNameSpace = http://WebXml.com.cn/
    • 查詢的方法名為 “getMobileCodeInfo”,需要帶上 “mobileCode” 與 “userID” 兩個(gè)參數(shù)。
    • 返回的結(jié)果存在 “getMobileCodeInfoResult” 中。
  3. 在 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 可以訪問(wèn)其 WSDL 說(shuō)明。

3. 資源文件布局
  1. 創(chuàng)建 activity_web_client.xml 文件,定義界面布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:orientation="vertical"tools:context="com.example.webservicephonelocationlookup.WebClient"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="輸入手機(jī)號(hào):" /><EditTextandroid:layout_width="150dp"android:layout_height="wrap_content"android:id="@+id/etphone" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="搜索"android:id="@+id/btnsearch" /></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查詢結(jié)果:" /><TextViewandroid:id="@+id/tvinfo"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
</LinearLayout>
4. Java 代碼
  1. 創(chuàng)建 WebClient.java 文件,實(shí)現(xiàn) WebService 調(diào)用邏輯。
package com.example.webservicephonelocationlookup;import android.os.AsyncTask;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;public class WebClient extends AppCompatActivity {private static final String SERVER_URL = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";private static final String NAMESPACE = "http://WebXml.com.cn/";private static final String METHOD_NAME = "getMobileCodeInfo";private EditText etPhone;private Button btnSearch;private TextView tvInfo;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_web_client);etPhone = (EditText) findViewById(R.id.etphone);btnSearch = (Button) findViewById(R.id.btnsearch);tvInfo = (TextView) findViewById(R.id.tvinfo);btnSearch.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String phoneNumber = etPhone.getText().toString();if (phoneNumber.length() > 0) {getPhoneLocation(phoneNumber);}}});}private void getPhoneLocation(String phoneNumber) {new AsyncTask<String, Void, String>() {@Overrideprotected String doInBackground(String... params) {String location = "";final HttpTransportSE httpSe = new HttpTransportSE(SERVER_URL);httpSe.debug = true;SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);soapObject.addProperty("mobileCode", params[0]);soapObject.addProperty("userID", "");final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);envelope.setOutputSoapObject(soapObject);envelope.dotNet = true;// 獲取返回信息try {httpSe.call(NAMESPACE + METHOD_NAME, envelope);if (envelope.getResponse() != null) {SoapObject result = (SoapObject) envelope.bodyIn;location = result.getProperty("getMobileCodeInfoResult").toString();}} catch (XmlPullParserException | SoapFault | IOException e) {e.printStackTrace();}return location;}@Overrideprotected void onPostExecute(String result) {tvInfo.setText(result);}}.execute(phoneNumber);}
}
5. AndroidManifest.xml 配置
  1. AndroidManifest.xml 中添加 INTERNET 權(quán)限。
<uses-permission android:name="android.permission.INTERNET"/>
  1. 配置應(yīng)用程序的入口 Activity。
<activity android:name="com.example.webservice.WebClient"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>
</activity>
6. 運(yùn)行效果
  • 在模擬器或真機(jī)上運(yùn)行應(yīng)用程序,輸入手機(jī)號(hào)碼,點(diǎn)擊搜索按鈕,查看查詢結(jié)果。

image-20231117151014751


【拓展】編寫(xiě) Android 程序,實(shí)現(xiàn)使用系統(tǒng)內(nèi)置游覽器打開(kāi)指定網(wǎng)頁(yè)。

步驟 1: 創(chuàng)建新項(xiàng)目
  1. 打開(kāi) Android Studio,選擇 “Start a new Android Studio project”。
  2. 選擇 “Empty Activity” 模板,點(diǎn)擊 “Next”。
  3. 命名項(xiàng)目為 “WebBrowserDemo”,選擇語(yǔ)言為 “Java”,點(diǎn)擊 “Finish”。
步驟 2: 修改布局文件
  1. 打開(kāi) activity_main.xml 文件,用以下代碼替換其中的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical"><EditTextandroid:id="@+id/ed"android:layout_width="match_parent"android:layout_height="200px"></EditText><Buttonandroid:id="@+id/bu1"android:layout_width="286dp"android:layout_height="wrap_content"android:text="Go" /><WebViewandroid:id="@+id/webView1"android:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1"android:focusable="false" />
</LinearLayout>
步驟 3: 編寫(xiě) Java 代碼
  1. 打開(kāi) MainActivity.java 文件,用以下代碼替換其中的內(nèi)容:
package com.example.webbrowserdemo;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity{private WebView webView; //聲明 WebView 組件的對(duì)象String url="";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);webView=(WebView)findViewById(R.id.webView1); //獲取WebView 組件webView.getSettings().setJavaScriptEnabled(true); //設(shè)置 JavaScript可用webView.setWebChromeClient(new WebChromeClient()); //處理JavaScript 對(duì)話框webView.setWebViewClient(new WebViewClient()); //處理各種通知和請(qǐng)求事件,如果不使用該句代碼,將使用內(nèi)置瀏覽器訪問(wèn)網(wǎng)頁(yè)webView.setInitialScale(57*4); //放網(wǎng)頁(yè)內(nèi)容放大 4 倍Button bu1=(Button)findViewById(R.id.bu1);EditText editText=findViewById(R.id.ed);bu1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {url=editText.getText().toString();openUrl(url);}});}//打開(kāi)網(wǎng)頁(yè)的方法private void openUrl(String id){if (!url.equals("")){webView.loadUrl("http://"+id+"/"); //}else {Toast.makeText(this,"網(wǎng)址不能為空",Toast.LENGTH_LONG).show();}}
}
步驟 4: 運(yùn)行應(yīng)用
  1. 運(yùn)行應(yīng)用程序,點(diǎn)擊 “打開(kāi)網(wǎng)頁(yè)” 按鈕。

  2. 系統(tǒng)將使用內(nèi)置瀏覽器打開(kāi)指定網(wǎng)頁(yè)。

    image-20231117171143765

【拓展】編寫(xiě) Android 程序,實(shí)現(xiàn)從指定網(wǎng)站下載文件。

步驟 1: 創(chuàng)建新的 Android 項(xiàng)目
  1. 打開(kāi) Android Studio。
  2. 選擇 “Start a new Android Studio project”。
  3. 選擇 “Empty Activity” 模板,然后點(diǎn)擊 “Finish”。
步驟 2: 修改布局文件

打開(kāi) res/layout/activity_main.xml 文件,并使用以下 XML 代碼替換默認(rèn)的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/downloadButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="下載文件" />
</RelativeLayout>
步驟 3: 在 MainActivity.java 中添加代碼

打開(kāi) MainActivity.java 文件,修改 onCreate 方法和添加新的方法:

package com.example.filedownloader;import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;public class MainActivity extends AppCompatActivity {private static final String DOWNLOAD_URL = "https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=%E7%99%BE%E5%BA%A6%E5%9B%BE%E7%89%87&step_word=&lid=7733045057659531704&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=505978886,3280506511&os=2821336839,1523677687&simid=3395585618,291075366&pn=0&rn=1&di=7264239678495129601&ln=1594&fr=&fmq=1700213057065_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=1e&objurl=https%3A%2F%2Fp3.itc.cn%2Fq_70%2Fimages03%2F20211117%2F1270baf1c2f84fa19a99ef82c52d454c.png&rpstart=0&rpnum=0&adpicid=0&nojc=undefined&dyTabStr=MCwxLDIsMyw2LDQsNSw4LDcsOQ%3D%3D";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button downloadButton = findViewById(R.id.downloadButton);downloadButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {new DownloadFileTask().execute(DOWNLOAD_URL);}});}private class DownloadFileTask extends AsyncTask<String, Void, Boolean> {@Overrideprotected Boolean doInBackground(String... urls) {String fileUrl = urls[0];try {URL url = new URL(fileUrl);HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();urlConnection.connect();InputStream inputStream = urlConnection.getInputStream();int totalSize = urlConnection.getContentLength();int downloadedSize = 0;byte[] buffer = new byte[1024];int bufferLength;String fileName = "示例圖片.png"; // 文件保存的名稱FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + fileName);while ((bufferLength = inputStream.read(buffer)) > 0) {fileOutputStream.write(buffer, 0, bufferLength);downloadedSize += bufferLength;}fileOutputStream.close();return true;} catch (IOException e) {e.printStackTrace();return false;}}@Overrideprotected void onPostExecute(Boolean result) {if (result) {Toast.makeText(MainActivity.this, "文件下載成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "文件下載失敗", Toast.LENGTH_SHORT).show();}}}
}
步驟 4: 添加 Internet 和存儲(chǔ)權(quán)限

確保在 AndroidManifest.xml 文件中添加了 Internet 和存儲(chǔ)權(quán)限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
步驟 5: 運(yùn)行應(yīng)用

image-20231117172551780

http://m.aloenet.com.cn/news/34802.html

相關(guān)文章:

  • c 做網(wǎng)站源碼實(shí)例seo入門(mén)教學(xué)
  • 大連專業(yè)網(wǎng)站建設(shè)東莞谷歌推廣
  • 網(wǎng)站建設(shè)外包合同seo外鏈發(fā)布
  • 做網(wǎng)站需要哪些費(fèi)用seo排名助手
  • 怎么優(yōu)化網(wǎng)站公司網(wǎng)站設(shè)計(jì)報(bào)價(jià)
  • 百度 網(wǎng)站地圖怎么做搜索引擎優(yōu)化百度百科
  • 怎么樣檢查網(wǎng)站有沒(méi)有做全站301鄭州網(wǎng)站策劃
  • 科技局網(wǎng)站建設(shè)方案百度關(guān)鍵詞價(jià)格
  • 科技網(wǎng)站設(shè)計(jì)愛(ài)站網(wǎng)關(guān)鍵字挖掘
  • 2015做啥網(wǎng)站能致富百度權(quán)重查詢
  • 邯鄲做網(wǎng)站推廣的公司桔子seo
  • 門(mén)戶網(wǎng)站建設(shè) 簡(jiǎn)報(bào)網(wǎng)絡(luò)廣告策劃書(shū)范文
  • 怎么做淘寶客導(dǎo)購(gòu)網(wǎng)站百度問(wèn)問(wèn)首頁(yè)登錄
  • 中國(guó)建設(shè)銀行招聘長(zhǎng)沙seo代理商
  • 制作自己網(wǎng)站有什么用官方網(wǎng)站百度一下
  • 便宜網(wǎng)站建設(shè)價(jià)格微信引流推廣精準(zhǔn)粉
  • 百度資料怎么做網(wǎng)站網(wǎng)站建設(shè)對(duì)企業(yè)品牌價(jià)值提升的影響
  • 什么行業(yè)做網(wǎng)站深圳網(wǎng)絡(luò)推廣有幾種方法
  • 網(wǎng)站項(xiàng)目建設(shè)目標(biāo)免費(fèi)下載b站視頻軟件
  • 網(wǎng)絡(luò)工作室是干嘛的seo系統(tǒng)源碼
  • 軟件開(kāi)發(fā)公司網(wǎng)站設(shè)計(jì)許昌seo推廣
  • 做商城網(wǎng)站要哪些流程如何創(chuàng)建自己的域名
  • 連云港做鴨網(wǎng)站2022最新新聞
  • 屏蔽蜘蛛網(wǎng)站還會(huì)被收錄嗎南京網(wǎng)站設(shè)計(jì)公司大全
  • 濟(jì)南做html5網(wǎng)站建設(shè)發(fā)布信息的免費(fèi)平臺(tái)有哪些
  • 專業(yè)做網(wǎng)站網(wǎng)站seo搜索引擎優(yōu)化教程
  • 百度廣告聯(lián)盟看廣告賺錢(qián)seo項(xiàng)目經(jīng)理
  • 什么是軟件定制開(kāi)發(fā)免費(fèi)seo搜索優(yōu)化
  • 投資做個(gè)app要多少錢(qián)長(zhǎng)沙seo優(yōu)化排名
  • 福州網(wǎng)站建設(shè)電話天津谷歌優(yōu)化