安卓手機(jī)app開發(fā)-調(diào)用網(wǎng)絡(luò)接口寫入mysql數(shù)據(jù)---kotlin版

花了點(diǎn)時(shí)間寫了一個(gè)入門級(jí)數(shù)據(jù)讀寫操作探孝,是kotlin版本改橘。教程沒有涉及json的第三方解析,沒有MVVM模式操作數(shù)據(jù)庫横媚,所以代碼很適合了解手機(jī)開發(fā)的app是如何操作數(shù)據(jù)庫的入門者閱讀棕诵。

? 教程是采用android studio最新版下編寫的纪蜒,項(xiàng)目是用底部導(dǎo)航欄的系統(tǒng)默認(rèn)模板搭建的蹭秋,很容易閱讀和理解扰付。寫入是通過遠(yuǎn)程php的接口寫入到數(shù)據(jù)庫的沒有用kotlin實(shí)現(xiàn)堤撵,因?yàn)橛胮hp實(shí)現(xiàn)后端數(shù)據(jù)庫操作是非常有優(yōu)勢(shì)的仁讨,編寫代碼快捷省事。完整源碼打包到? 專業(yè)開發(fā)網(wǎng) http://www.zhuanyekaifa.com/datarwite.rar 去下載吧


看幾個(gè)效果圖:


項(xiàng)目結(jié)構(gòu)圖:紅框部分是著重改寫的部分

2 , 布局文件

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.dashboard.DashboardFragment">

<EditText

? ? android:id="@+id/personname"

? ? android:layout_width="wrap_content"

? ? android:layout_height="wrap_content"

? ? android:layout_marginStart="24dp"

? ? android:layout_marginTop="130dp"

? ? android:layout_marginEnd="24dp"

? ? android:ems="10"

? ? android:inputType="textPersonName"

? ? android:text="輸入姓名"

? ? app:layout_constraintEnd_toEndOf="parent"

? ? app:layout_constraintStart_toStartOf="parent"

? ? app:layout_constraintTop_toTopOf="parent" />

<EditText

? ? android:id="@+id/personage"

? ? android:layout_width="wrap_content"

? ? android:layout_height="wrap_content"

? ? android:layout_marginTop="16dp"

? ? android:ems="10"

? ? android:inputType="number"

? ? android:text="輸入年齡"

? ? app:layout_constraintEnd_toEndOf="parent"

? ? app:layout_constraintStart_toStartOf="parent"

? ? app:layout_constraintTop_toBottomOf="@+id/personname" />

<EditText

? ? android:id="@+id/personadd"

? ? android:layout_width="wrap_content"

? ? android:layout_height="wrap_content"

? ? android:layout_marginTop="16dp"

? ? android:ems="10"

? ? android:inputType="textPersonName"

? ? android:text="輸入住址"

? ? app:layout_constraintEnd_toEndOf="parent"

? ? app:layout_constraintStart_toStartOf="parent"

? ? app:layout_constraintTop_toBottomOf="@+id/personage" />

<ImageButton

? ? android:id="@+id/imageButton"

? ? android:layout_width="wrap_content"

? ? android:layout_height="wrap_content"

? ? android:layout_marginTop="16dp"

? ? android:src="@android:drawable/ic_input_add"

? ? app:layout_constraintEnd_toEndOf="parent"

? ? app:layout_constraintStart_toStartOf="parent"

? ? app:layout_constraintTop_toBottomOf="@+id/personadd" />


3.數(shù)據(jù)請(qǐng)求实昨,主要是看是如何請(qǐng)求遠(yuǎn)程數(shù)據(jù)的部分

package com.mykotlin.five_adddata.ui.dashboard

import android.os.Bundle

import android.os.Looper

import android.text.Editable

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import android.widget.Toast

import androidx.fragment.app.Fragment

import androidx.lifecycle.ViewModelProvider

import com.mykotlin.five_adddata.databinding.FragmentDashboardBinding

import okhttp3.*

import org.json.JSONObject

import java.io.IOException

class DashboardFragment : Fragment() {

private var dashboardViewModel: DashboardViewModel? = null

private var _binding: FragmentDashboardBinding? = null

// This property is only valid between onCreateView and

// onDestroyView.

private val binding get() = _binding!!

override fun onCreateView(

? ? inflater: LayoutInflater,

? ? container: ViewGroup?,

? ? savedInstanceState: Bundle?

? ? ): View? {

? ? dashboardViewModel =

? ? ? ? ViewModelProvider(this).get(DashboardViewModel::class.java)

? ? _binding = FragmentDashboardBinding.inflate(inflater, container, false)

? ? val root: View = binding.root

? ? val pname = binding.personname.text

? ? val page = binding.personage.text

? ? val padd=binding.personadd.text

? ? binding.imageButton.setOnClickListener {

? ? ? ? getAdd(pname,page,padd)

? ? }

? ? return root

}

private fun getAdd(name: Editable, age: Editable, add: Editable){

? ? **val client = OkHttpClient()

? ? var url = "http://www.kuaidian777.com/mydata.php"

? ? val urlAPI: String = url**

? ? val builder = FormBody.Builder()

? ? builder.add("hname", name.toString())

? ? builder.add("hage", age.toString())

? ? builder.add("hadd", add.toString())

? ? val formBody = builder.build()

? ? val request = Request.Builder()

? ? ? ? .method("POST", formBody)

? ? ? ? .url(urlAPI).build()

? ? client.newCall(request).enqueue(object : Callback {

? ? ? ? ? ? override fun onResponse(call: Call, response: Response) {

? ? ? ? ? ? ? ? val result = response.body?.string()

? ? ? ? ? ? ? ? var jstr=""

? ? ? ? ? ? ? ? var myjson= JSONObject(result)

? ? ? ? ? ? ? ? //println("result:" + myjson)

? ? ? ? ? ? ? ? if(myjson.getString("msg")=="ok") {

? ? ? ? ? ? ? ? ? ? Looper.prepare();

? ? ? ? ? ? ? ? ? ? Toast.makeText(context, "輸入寫入成功", Toast.LENGTH_SHORT).show()

? ? ? ? ? ? ? ? ? ? Looper.loop();

? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? Looper.prepare();

? ? ? ? ? ? ? ? ? ? Toast.makeText(context, "唉洞豁,失敗了", Toast.LENGTH_SHORT).show()

? ? ? ? ? ? ? ? ? ? Looper.loop();

? ? ? ? ? ? ? ? ? ? //println(myjson.getString("msg"))

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? override fun onFailure(call: Call, e: IOException) {

? ? ? ? ? ? ? ? println("Failed request api :( " + e.message)

? ? ? ? ? ? }

? ? })

}

override fun onDestroyView() {

? ? super.onDestroyView()

? ? _binding = null

}


}

4.點(diǎn)擊 +按鈕后 把數(shù)據(jù)寫入數(shù)據(jù)庫,通過調(diào)用遠(yuǎn)程http://www.kuaidian777.com/mydata.php寫入的

接口內(nèi)容

<? header('Content-Type:application/json; charset=utf-8'); $mysql_server_name = 'localhost'; //改成自己的mysql數(shù)據(jù)庫服務(wù)器?

$mysql_username = '荒给。丈挟。。志电。曙咽。'; //改成自己的mysql數(shù)據(jù)庫用戶名

?$mysql_password = '。挑辆。例朱。。鱼蝉。'; //改成自己的mysql數(shù)據(jù)庫密碼

?$mysql_database = '洒嗤。。魁亦。渔隶。。'; //改成自己的mysql數(shù)據(jù)庫名 $conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //連接數(shù)據(jù)庫 //連接數(shù)據(jù)庫錯(cuò)誤提示

?if (mysqli_connect_errno($conn)) {?

die("連接 MySQL 失敗: " . mysqli_connect_error());?

}?

mysqli_query($conn,"set names utf8"); //數(shù)據(jù)庫編碼格式

?$hname=$_POST["hname"]; $hage=$_POST["hage"];;?

$hadd=$_POST["hadd"];;

?$sql="INSERT INTO `testme`(`hname`, `hage`, `hadd`) VALUES ('".$hname."',".$hage.",'".$hadd."')";?

mysqli_query($conn,$sql);

??>

技術(shù)探討 : qq 2047879076

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末洁奈,一起剝皮案震驚了整個(gè)濱河市间唉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌利术,老刑警劉巖终吼,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異氯哮,居然都是意外死亡际跪,警方通過查閱死者的電腦和手機(jī)商佛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來姆打,“玉大人良姆,你說我怎么就攤上這事♂O罚” “怎么了玛追?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長闲延。 經(jīng)常有香客問我痊剖,道長,這世上最難降的妖魔是什么垒玲? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任陆馁,我火速辦了婚禮,結(jié)果婚禮上合愈,老公的妹妹穿的比我還像新娘叮贩。我一直安慰自己,他們只是感情好佛析,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布益老。 她就那樣靜靜地躺著,像睡著了一般寸莫。 火紅的嫁衣襯著肌膚如雪捺萌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天膘茎,我揣著相機(jī)與錄音桃纯,去河邊找鬼。 笑死辽狈,一個(gè)胖子當(dāng)著我的面吹牛慈参,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播刮萌,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼驮配,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了着茸?” 一聲冷哼從身側(cè)響起壮锻,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎涮阔,沒想到半個(gè)月后猜绣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敬特,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年掰邢,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了牺陶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡辣之,死狀恐怖掰伸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情怀估,我是刑警寧澤狮鸭,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站多搀,受9級(jí)特大地震影響歧蕉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜康铭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一惯退、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧麻削,春花似錦蒸痹、人聲如沸春弥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匿沛。三九已至扫责,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間逃呼,已是汗流浹背鳖孤。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留抡笼,地道東北人苏揣。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像推姻,于是被迫代替她去往敵國和親平匈。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354