Android向服务器请求界面,53. (android开发)使用OkHttp向服务器提交数据的get和post方法...

android客户端向服务端提交数据,在HTTP协议下,可以采用get或post方法。

首先构造一个界面

由于我要访问的服务器地址是IP地址,就需要一个能够包含IP变化带来的动态情况。所以采用了NumberPicker方式,用四个这样的组件构成服务器地址选择器。

e5d10ffc0dc2

IP选择器

再有一个填写附加信息的地方,和两个按钮,一个按钮使用GET方法,一个按钮使用POST方法。

e5d10ffc0dc2

操作界面

局域网测试,服务端IP为 192.168.1.105

所以界面的设置文件是这样的

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"

android:orientation="vertical"

tools:context="com.cofox.mykt.myweather.OkHttpActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/ip_n1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true">

android:id="@+id/ip_n2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true">

android:id="@+id/ip_n3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true">

android:id="@+id/ip_n4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true">

android:id="@+id/edttxtURL"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="输入传送信息" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scrollbars="horizontal">

android:id="@+id/btnOkHttpResponseGet"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="OkHttp发送请求和获取响应(GET)"

android:textAllCaps="false" />

android:id="@+id/btnOkHttpResponsePost"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="OkHttp发送请求和获取响应(POST)"

android:textAllCaps="false" />

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/ttviewResponse"

android:layout_width="match_parent"

android:layout_height="match_parent" />

设置服务端IP地址的默认值和IP地址拼串。

设置4个NumberPicker的最大最小值和当前值。

//设置访问服务端IP

var serverIp = "192.168.1.105"

val ipMinNumber = 0

val ipMaxNumber = 255

fun setServerIp(i0:String,i1:String,i2:String,i3:String) {

serverIp = i0 + "." + i1 + "." + i2 + "." + i3

}

在onCreate代码中,

ip_n1.minValue = ipMinNumber

ip_n1.maxValue = ipMaxNumber

ip_n1.value = 192

ip_n2.minValue = ipMinNumber

ip_n2.maxValue = ipMaxNumber

ip_n2.value = 168

ip_n3.minValue = ipMinNumber

ip_n3.maxValue = ipMaxNumber

ip_n3.value = 1

ip_n4.minValue = ipMinNumber

ip_n4.maxValue = ipMaxNumber

ip_n4.value = 105

ip_n1.setOnValueChangedListener { numberPicker, i, j ->

ip_n1.value = j

setServerIp(ip_n1.value.toString(),ip_n2.value.toString(),ip_n3.value.toString(),ip_n4.value.toString())

}

ip_n2.setOnValueChangedListener { numberPicker, i, j ->

ip_n2.value = j

setServerIp(ip_n1.value.toString(),ip_n2.value.toString(),ip_n3.value.toString(),ip_n4.value.toString())

}

ip_n3.setOnValueChangedListener { numberPicker, i, j ->

ip_n3.value = j

setServerIp(ip_n1.value.toString(),ip_n2.value.toString(),ip_n3.value.toString(),ip_n4.value.toString())

}

ip_n4.setOnValueChangedListener { numberPicker, i, j ->

ip_n4.value = j

setServerIp(ip_n1.value.toString(),ip_n2.value.toString(),ip_n3.value.toString(),ip_n4.value.toString())

}

这段代码还是很有优化空间的,但这次就这么用吧。

get方法传递数据相对简单,因为如果有数据要传的话,也是拼串。所以就不写详细的拼的过程了,只是将来记得写完整的拼好的服务端地址。

//get方式发送数据

btnOkHttpResponseGet.setOnClickListener {

Thread {

try {

val client = OkHttpClient()

//设置目标网络地址

val request = Request.Builder().url("http://" + serverIp).build()

//向服务端发送请求

val response = client.newCall(request).execute()

//获取响应数据

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

runOnUiThread { ttviewResponse.text = responseStr }

} catch (e: Exception) {

runOnUiThread { Toast.makeText(this, e.message, Toast.LENGTH_LONG).show() }

}

}.start()

}

post方法相比get来说,要写一个FormBody

//post方式发送数据

btnOkHttpResponsePost.setOnClickListener {

Thread {

try {

var str = ""

str = edttxtURL.text.toString()

//封装post请求数据

val requestBody = FormBody.Builder()

.add("name", "Joel" + str)

.add("age", "27")

.build()

val client = OkHttpClient()

val request = Request.Builder().url("http://" + serverIp).post(requestBody).build()

val response = client.newCall(request).execute()

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

runOnUiThread { ttviewResponse.text = responseStr }

} catch (e: Exception) {

runOnUiThread { Toast.makeText(this, e.message, Toast.LENGTH_LONG).show() }

}

}.start()

}

e5d10ffc0dc2

界面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值