Android 科特林邮政请求
我是科特林的新手。我想问一下邮寄申请 我想将“nim”、“nama”和“address”edittext.text传递到数据库。但在我的代码中,Toast“Error Occured”仍然出现,数据库没有获取任何数据。那我该怎么办Android 科特林邮政请求,android,api,post,kotlin,request,Android,Api,Post,Kotlin,Request,我是科特林的新手。我想问一下邮寄申请 我想将“nim”、“nama”和“address”edittext.text传递到数据库。但在我的代码中,Toast“Error Occured”仍然出现,数据库没有获取任何数据。那我该怎么办 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val jsonobj = JSONObject()
jsonobj.put("nim", nim_editText.text)
jsonobj.put("nama", nama_editText.text)
jsonobj.put("address", address_editText.text)
val queue = Volley.newRequestQueue(this)
val url = "http://192.168.100.7/simplecrud/create.php"
val req = JsonObjectRequest(Request.Method.POST,url,jsonobj,
Listener {
Toast.makeText(this@MainActivity, "Success", Toast.LENGTH_SHORT).show()
},
ErrorListener {
Toast.makeText(this@MainActivity, "Error Occured", Toast.LENGTH_SHORT).show()
})
val btn = findViewById<Button>(R.id.button1)
btn.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
queue.add(req)
}
})
override-fun-onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val jsonobj=JSONObject()
jsonobj.put(“nim”,nim_editText.text)
jsonobj.put(“nama”,nama_editText.text)
jsonobj.put(“地址”,address_editText.text)
val queue=Volley.newRequestQueue(此)
val url=”http://192.168.100.7/simplecrud/create.php"
val req=JsonObjectRequest(Request.Method.POST、url、jsonobj、,
听众{
Toast.makeText(this@MainActivity,“成功”,吐司。长度(短)。show()
},
错误侦听器{
Toast.makeText(this@MainActivity,“发生错误”,Toast.LENGTH\u SHORT.show()
})
val btn=findViewById(R.id.button1)
btn.setOnClickListener(对象:View.OnClickListener{
覆盖有趣的onClick(v:视图?){
队列添加(req)
}
})
这是我的create.php代码
<?php
require_once('connection.php');
$nim = $_POST['nim'];
$name = $_POST['name'];
$address = $_POST['address'];
$gender = $_POST['gender'];
if(!$nim || !$name || !$address || !$gender ){
echo json_encode(array('message'=>'required field is empty.'));
}
else{
$query = mysqli_query($CON, "INSERT INTO tb_student VALUES ('$nim','$name','$address','$gender')");
if($query){
echo json_encode(array('message'=>'student data successfully added.'));
}
else{
echo json_encode(array('message'=>'student data failed to add.'));
}
}
?>
确保您正在阅读文档的是您的php文件,出于已完成的原因,大多数总计不会添加此项,但php不再看到android post和get标题。
我会在休息的15分钟内更新一个链接,以供参考
通过Log.d(标记“Error:+it.getMessage())发布您的“截击错误”对象
在ErrorListener的内部。并且,nim_editText.text将返回editText的当前状态。如果您想发送输入的数据,您应该在用户单击按钮时创建JSONObject。谢谢,我会尝试。但是在代码的底部,有一个OnClickListener来执行请求我的意思是->我复制了您的代码,但它仍然是一样的“发生错误"你能调试它吗?它必须包含一些关于错误的信息。当我在postman上运行它时,我的php文件很好,它可以从postman传递数据。但是当我想从kotlin应用程序输入数据时,我感到困惑。这就是我在运行诸如postman ect之类的测试时所说的。它将测试良好。当你从android连接到指向php文件头和请求方法的发送方式与以前不同。不确定原因。您从未收到响应代码200,因为您的php文件的读数为空。因此,它每次都会发送错误代码,因为数据库中没有可输入的内容。发布您的php代码,我将尽力提供帮助。我在前一两周经历了这一痛苦的过程我明白了。谢谢。我已经把我的php代码发布在我编辑的问题上了。
fun post(url: String, body: String): String {
return URL(url)
.openConnection()
.let {
it as HttpURLConnection
}.apply {
setRequestProperty("Content-Type", "application/json; charset=utf-8")
requestMethod = "POST"
doOutput = true
val outputWriter = OutputStreamWriter(outputStream)
outputWriter.write(body)
outputWriter.flush()
}.let {
if (it.responseCode == 200) it.inputStream else it.errorStream
}.let { streamToRead ->
BufferedReader(InputStreamReader(streamToRead)).use {
val response = StringBuffer()
var inputLine = it.readLine()
while (inputLine != null) {
response.append(inputLine)
inputLine = it.readLine()
}
it.close()
response.toString()
}
}
}