《移动项目实践》实验报告——Android网络通信
生活随笔
收集整理的這篇文章主要介紹了
《移动项目实践》实验报告——Android网络通信
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
實驗?zāi)康?/h1>
掌握數(shù)據(jù)解析(以json為例)、網(wǎng)絡(luò)請求的關(guān)鍵內(nèi)容。
實驗內(nèi)容
1.接口示例(北京)http://www.weather.com.cn/data/sk/101010100.html
2.可選擇和任意切換北京、杭州、廣州、上海的天氣(陰晴雨雪)、氣溫、濕度等。
3.解析json接口獲取的數(shù)據(jù)。
4.在app中展現(xiàn)天氣結(jié)果。
實驗參考網(wǎng)址:https://blog.csdn.net/liji_xc/article/details/47323137
實驗過程(實驗的設(shè)計思路、關(guān)鍵源代碼等)
源代碼:https://gitee.com/shentuzhigang/mini-project/tree/master/android-network
package io.shentuzhigang.demo.networkimport androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.StrictMode import android.widget.TextView import com.alibaba.fastjson.JSON import okhttp3.OkHttpClient import okhttp3.Requestclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)if (android.os.Build.VERSION.SDK_INT > 9) {val policy = StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);}val str = run("http://www.weather.com.cn/data/sk/101210101.html")val json = JSON.parseObject(str).getJSONObject("weatherinfo")val textView = findViewById<TextView>(R.id.textView)textView.text = String.format("城市:%s\n風(fēng)向:%s\n風(fēng)力:%s\n濕度:%s\nAP:%s\n",json.getString("city"),json.getString("WD"),json.getString("WS"),json.getString("SD"),json.getString("AP"),)}var client = OkHttpClient()fun run(url:String): String? {val request = Request.Builder().url(url).build();val response = client.newCall(request).execute()return response.body?.string();} }實驗結(jié)果(實驗最終作品截圖說明)
實驗心得
參考文章
- android json解析使用總結(jié)(二)-—天氣預(yù)報的實現(xiàn)
、
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的《移动项目实践》实验报告——Android网络通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《移动项目实践》实验报告——Androi
- 下一篇: vuepress 2.x 集成百度统计