Home > Enterprise >  I am newbie in Android Studio. I have a task how to display the name automatically when the id / nik
I am newbie in Android Studio. I have a task how to display the name automatically when the id / nik

Time:12-29

At this time I'm having trouble writing java code. The concept in this program is when I fill in the NIK field and press the get button, the name (FIELD NAME) will appear automatically based on the data from database

Database Structure

NIK NAMA
96296 Farrasta
94878 Alfian

[Application View] (https://i.stack.imgur.com/x06Vy.png)

Error when i click get button

java.lang.illegalStateException : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Get Button

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();
        }
    });
}

Data Master

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 request

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

Php file

<?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 response

[{"NAMA":"Alfian U"}]

CodePudding user response:

Make this change in your data class :

    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;
}

}

Also make this changes in onClick of Get button :

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();
        }
    });
}

Also make change in this :

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