目前我在编写java代码时遇到了一些麻烦,这个程序的概念是当我填写NIK字段并按下get按钮时,名称(FIELDNAME)会根据数据库中的数据自动出现
数据库结构
| 尼克|非市场准入|
| - ------| - ------|
| 小行星96296|法拉斯塔|
| 小行星94878|阿尔菲安|
[应用程序视图](https://i.stack.imgur.com/x06Vy.png)
单击获取按钮时出错
java.lang.illegalStateException : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
获取按钮
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NikKry = etNik.getText().toString();
getNama();
}
});
private void getNama(){
APIRequestData armNama = RetroMaster.konekRetrofit().create(APIRequestData.class);
Call<DataMaster> tampilNama = armNama.ardGetNama(NikKry);
tampilNama.enqueue(new Callback<DataMaster>() {
@Override
public void onResponse(Call<DataMaster> call, Response<DataMaster> response) {
if (response.isSuccessful()){
etNama.setText(response.body().getNAMA());
}
}
@Override
public void onFailure(Call<DataMaster> call, Throwable t) {
Toast.makeText(TambahActivity.this, "Gagal "+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
主数据
package com.example.crudoracle.Model;
public class DataMaster {
private String NIK;
private String NAMA;
public String getNIK() {
return NIK;
}
public void setNIK(String NIK) {
this.NIK = NIK;
}
public String getNAMA() {
return NAMA;
}
public void setNAMA(String NAMA) {
this.NAMA = NAMA;
}
}
API请求
@FormUrlEncoded
@POST("getnama.php")
Call<DataMaster> ardGetNama(
@Field("NIK") String NIK
);
php文件
<?php
include ("koneksi.php");
$NIK = $_POST['NIK'];
$query = "SELECT NAMA FROM MASTER_KRY WHERE NIK = '$NIK'";
$eksekusi = oci_parse($conn, $query);
$cek = oci_execute($eksekusi);
$response = array();
while($ambil = oci_fetch_object($eksekusi)){
$F["NAMA"] = $ambil->NAMA;
array_push($response, $F);
}
echo json_encode($response);
oci_close($conn);
?>
Postman 回应
[{"NAMA":"Alfian U"}]
1条答案
按热度按时间gijlo24d1#
在数据类中进行以下更改:
也可在onClick of Get按钮中进行此更改:
还应更改以下内容: