php 我是Android Studio的新手,我有一个任务,如何根据数据库中的数据填充id / nik字段时自动显示名称

cnh2zyt3  于 2022-12-28  发布在  PHP
关注(0)|答案(1)|浏览(97)

目前我在编写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"}]
gijlo24d

gijlo24d1#

在数据类中进行以下更改:

package com.example.crudoracle.Model;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class DataMaster {

@SerializedName("NAMA")
@Expose
private String nama;

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

}

也可在onClick of Get按钮中进行此更改:

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<List<DataMaster>>() {
        @Override
        public void onResponse(Call<List<DataMaster>> call, Response<List<DataMaster>> response) {
            if (response.isSuccessful()){
                etNama.setText(response.body()[0].getNAMA());
            }
        }
        @Override
        public void onFailure(Call<List<DataMaster>> call, Throwable t) {
            Toast.makeText(TambahActivity.this, "Gagal "+t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

还应更改以下内容:

@FormUrlEncoded
@POST("getnama.php")
Call<List<DataMaster>> ardGetNama(
        @Field("NIK") String NIK
);

相关问题