POST data with retrofit2, Bearer token Unauthorized android

喜欢而已 提交于 2020-05-16 06:25:42

问题


I want to post new data to server with this JSON:

{
 "tgl_Lahir": "1990-12-18 00:00:00",
 "nama": "Joe",
 "keterangan": "Employee",
 "tempatLahir": "Los Angeles",
 "noPegawai": "111111",
 "golDarah": "0",
 "statusNikah": "0",
  "hubungans": {
            "id": "10"
         },
 "agama": {
            "id_Agama": "1"
          },

 "jeniskelamin": {
            "jenisKelamin": "1"
        }
}

Here's my ApiClientPOST.java:

public class ApiClientPOST {
    private static Retrofit retrofit = null;


    public static Retrofit getClient(String url){
        if(retrofit == null){
            retrofit = new Retrofit.Builder().baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }
}

Here's my APIUtils.java:

public class APIUtils {
    private APIUtils(){
    };

    public static final String API_URL = "IPAddress/employee/family/add";

    public static MainInterface getUserService(){
        return ApiClientPOST.getClient(API_URL).create(MainInterface.class);
    }

}

Here's my familylistresponsePOST.java:


public class familylistresponsePOST {

    @SerializedName("noPegawai")
    private String noPegawai;

    @SerializedName("date_otor")
    private Object dateOtor;

    @SerializedName("jeniskelamin")
    private Jeniskelamin jeniskelamin;

    @SerializedName("keterangan")
    private String keterangan;

    @SerializedName("hubungans")
    private Hubungans hubungans;

    @SerializedName("tgl_Lahir")
    private String tglLahir;

    @SerializedName("nama")
    private String nama;

    @SerializedName("agama")
    private Agama agama;

    @SerializedName("statusNikah")
    private String statusNikah;

    @SerializedName("tempatLahir")
    private String tempatLahir;

    @SerializedName("id")
    private int id;

    @SerializedName("golDarah")
    private String golDarah;

    public void setNoPegawai(String noPegawai){
        this.noPegawai = noPegawai;
    }

    public String getNoPegawai(){
        return noPegawai;
    }

    public void setDateOtor(Object dateOtor){
        this.dateOtor = dateOtor;
    }

    public Object getDateOtor(){
        return dateOtor;
    }

    public void setJeniskelamin(Jeniskelamin jeniskelamin){
        this.jeniskelamin = jeniskelamin;
    }

    public Jeniskelamin getJeniskelamin(){
        return jeniskelamin;
    }

    public void setKeterangan(String keterangan){
        this.keterangan = keterangan;
    }

    public String getKeterangan(){
        return keterangan;
    }

    public void setHubungans(Hubungans hubungans){
        this.hubungans = hubungans;
    }

    public Hubungans getHubungans(){
        return hubungans;
    }

    public void setTglLahir(String tglLahir){
        this.tglLahir = tglLahir;
    }

    public String getTglLahir(){
        return tglLahir;
    }

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

    public String getNama(){
        return nama;
    }

    public void setAgama(Agama agama){
        this.agama = agama;
    }

    public Agama getAgama(){
        return agama;
    }

    public void setStatusNikah(String statusNikah){
        this.statusNikah = statusNikah;
    }

    public String getStatusNikah(){
        return statusNikah;
    }

    public void setTempatLahir(String tempatLahir){
        this.tempatLahir = tempatLahir;
    }

    public String getTempatLahir(){
        return tempatLahir;
    }

    public void setId(int id){
        this.id = id;
    }

    public int getId(){
        return id;
    }

    public void setGolDarah(String golDarah){
        this.golDarah = golDarah;
    }

    public String getGolDarah(){
        return golDarah;
    }

    @Override
    public String toString(){
        return
                "ListUserResponse2{" +
                        "noPegawai = '" + noPegawai + '\'' +
                        ",date_otor = '" + dateOtor + '\'' +
                        ",jeniskelamin = '" + jeniskelamin + '\'' +
                        ",keterangan = '" + keterangan + '\'' +
                        ",hubungans = '" + hubungans + '\'' +
                        ",tgl_Lahir = '" + tglLahir + '\'' +
                        ",nama = '" + nama + '\'' +
                        ",agama = '" + agama + '\'' +
                        ",statusNikah = '" + statusNikah + '\'' +
                        ",tempatLahir = '" + tempatLahir + '\'' +
                        ",id = '" + id + '\'' +
                        ",golDarah = '" + golDarah + '\'' +
                        "}";
    }
}

I've tried to create this method and use it on my Button.setOnClickListener:

  public void addFamily(String noPegawai,String agama, String hubungan, String jenisKelamins, String tgl_Lahir, String nama, String keterangan, String tempatLahir, String golDarah, String statusNikah){
        SharedPreferences preferences = getSharedPreferences("MyPref",0);
        String tokens = preferences.getString("userToken",null);
        Call<familylistresponse> call = apiService.addFams(noPegawai,agama, hubungan, jenisKelamins, tgl_Lahir , nama, keterangan, tempatLahir, golDarah, statusNikah, "Bearer" + tokens);
        call.enqueue(new Callback<familylistresponse>() {
            @Override
            public void onResponse(Call<familylistresponse> call, Response<familylistresponse> response) {
//                if (response.isSuccessful()){
                    familylistresponse resultsData = new familylistresponse();
                    resultsData= response.body();
                    Toast.makeText(TambahDataKeluarga.this,"Data Berhasil Ditambahkan!" + resultsData, Toast.LENGTH_SHORT).show();
//                }
            }

            @Override
            public void onFailure(Call<familylistresponse> call, Throwable t) {
                Log.e("ERROR: ", t.getMessage());
            }
        });
    }

This one is my tambah Button:

tambah.setOnClickListener(v -> {

            SharedPreferences preferences = getSharedPreferences("MyPref",0);
            String noPegawai = preferences.getString("noPegawai",null);
            String snopeg = etNoPegawai.getText().toString().trim();
            String snama = etNama.getText().toString().trim();
            String stmpLahir = etTmptLahir.getText().toString().trim();
            String stglLahir = etTglLahir.getText().toString().trim();
            String sketerangan = etKeterangan.getText().toString().trim();
            String sgoldar = etGoldar.getText().toString().trim();
            String sstatusnikah = etStatusNikah.getText().toString().trim();

            valueJenisKelamin = jeniskelamin.getSelectedItem().toString();
            valueHubungan = spHubungans.getSelectedItem().toString();
            valueAgama = spAgama.getSelectedItem().toString();

            familylistresponse f = new familylistresponse();
            f.setNoPegawai(snopeg);
            agamas.setAgama(spAgama.getSelectedItem().toString().trim());
            jks.setJenisKelamin(jeniskelamin.getSelectedItem().toString().trim());
            hubungans.setHubungan(spHubungans.getSelectedItem().toString().trim());
            addFamily(snopeg, valueAgama, valueHubungan, valueJenisKelamin, stglLahir, snama,  sketerangan, stmpLahir, sgoldar, sstatusnikah);
            Log.d(f.getNama(),f.getGolDarah());

            Toast.makeText(TambahDataKeluarga.this,"No pegawai "+ noPegawai + " Nama Pegawai "+ snama+ " Tgl Lahir "+ stglLahir
                        + " Agama " + valueAgama
                        + " Hubungan " + valueHubungan
                        + " Jenis Kelamin " + valueJenisKelamin
                        + " Tgl Lahir " + stglLahir
                        + " Keterangan " + sketerangan
                        + " Tempat Lahir " + stmpLahir
                        + " Goldar " + sgoldar
                        + " Status Nikah " + sstatusnikah,Toast.LENGTH_LONG).show();

        });

The toast says that the data is successfully stored but in fact, it isn't. The toast also says that response.body() is null and there is no error in logcat even in the debugger. Please kindly help me. Thanks in advance for any help


回答1:


I do not see where you are defining Hubungans, Agama and Jeniskelamin classes although you are using it as datatype inside your familylistresponsePOST.java

After creating these three classes, I hope your issue will be solved.



来源:https://stackoverflow.com/questions/61341571/post-data-with-retrofit2-bearer-token-unauthorized-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!