Saya tahu ini bukan pertanyaan pertama tentang kesalahan ini. Saya telah mencari jawabannya di mana-mana tetapi saya tidak dapat menemukan solusinya. ketika saya menjalankan kode saya mendapatkan pengecualian ini;

java.lang.IllegalStateException: Diharapkan BEGIN_ARRAY tetapi BEGIN_OBJECT pada jalur 1 kolom 2 jalur $

Aktifitas utama

public class MainActivity extends AppCompatActivity {
    ArrayList<Doviz> dovizs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl("https://finans.truncgil.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DovizApi dovizApi = retrofit.create(DovizApi.class);

        Call<List<Doviz>> call = dovizApi.getDoviz();

      

        call.enqueue(new Callback<List<Doviz>>() {

            @Override
            public void onResponse(Call<List<Doviz>> call, Response<List<Doviz>> response) {
                System.out.println("deneme");
                List<Doviz> responseList= response.body();
                dovizs = new ArrayList<>(responseList);
                System.out.println(dovizs.get(0).USD);

            }

            @Override
            public void onFailure(Call<List<Doviz>> call, Throwable t) {
                System.out.println(t.fillInStackTrace());
            }
        });
    }
}

Kelas Jawa

public class Doviz {

    @SerializedName("Buying")
    public String USD;

}

Antarmuka

public interface DovizApi {
    @GET("today.json")
    Call<List<Doviz>> getDoviz();
}

Daftar yang saya gunakan https://finans.truncgil.com/v3/today.json

Saya telah berurusan dengan ini selama berhari-hari. Saya harap kita bisa menyelesaikannya

0
Doğukan Çiftçi 12 Mei 2021, 15:28

2 jawaban

Jawaban Terbaik

Apa yang Terjadi?

  • Akar dari respons API adalah JSONObject dan bukan JSONArray.
  • v3 hilang dari URL API.

Solusi

Perbarui Antarmuka

public interface DovizApi {
    @GET("v3/today.json")
    Call<HashMap<String, Object>> getDoviz();
}

Perbarui Model Respons (Doviz)

public class Doviz {
   @SerializedName("Buying")
   public String buying;

   // Similarly you can add other keys here
}

Perbarui Panggilan Retrofit

Call<HashMap<String, Object>> call = dovizApi.getDoviz();
call.enqueue(new Callback<HashMap<String, Object>>() {

        @Override
        public void onResponse(Call<HashMap<String, Object>> call, Response<HashMap<String, Object>> response) {
            HashMap<String, Object> responseModel = response.body();

            // Iterate all the currencies
            for (Map.Entry<String, Object> entry : responseModel.entrySet())
                if(entry.getValue() instanceof LinkedTreeMap) { // Since the first element is of type String i.e. "Update_Date"
                    Doviz doviz = new Gson().fromJson(new Gson().toJson(((LinkedTreeMap<String, Object>) entry.getValue())), Doviz.class);
                    System.out.println("Currency = " + entry.getKey() +
                                     ", Buying = " + doviz.buying);
                }
        }

        @Override
        public void onFailure(Call<HashMap<String, Object>> call, Throwable t) {
            System.out.println(t.fillInStackTrace());
        }
    });
1
Navjot Singh Bedi 13 Mei 2021, 07:21

Titik pemeriksaan

  1. Tipe

Json terlihat seperti di bawah ini

{
   "Update_Date":"2021-05-12 16:00:01",
   "USD":{
      "Buying":"8,3681",
      "Type":"Currency",
      "Selling":"8,3735",
      "Change":"%1,13"
   },
   ...
}

Ini bukan <List> dan USD adalah salah satu kunci di json

Jadi Call<List<Doviz>> harus berubah menjadi Call<Doviz>

  1. DTO (Doviz)

Anda mungkin menginginkan Buying nilai USD. Anda DTO(Doviz) harus memiliki properti sebagai DTO lain seperti ini

  class Doviz {
        @SerializedName("USD")
        CurrencyInfo usd;
       
        // else like this
        @SerializedName("EUR")
        CurrencyInfo eur;

        //...
  }

  class CurrencyInfo {
        //if Path(/v3) included
        @SerializedName("Buying")
        String buying;

        //if Path (/v3) not included 
        @SerializedName("Alış")
        String _buying;

       // else.. 
 }

interface DovizApi{
        @GET("/v3/today.json")
        fun getEnDoviz(): Call<Doviz>
}
Call<Doviz> call = dovizApi.getDoviz();
call.enqueue(new Callback<Doviz>(){
            @Override
            public void onResponse(Call<Doviz> call,Response<Doviz>response){
                Doviz doviz = response.body();
                CurrencyInfo info = doviz.getUsd();
                System.out.println(info.getBuying());
            }
..
}

Ini bekerja dengan baik.

0
liveAnyway 12 Mei 2021, 21:44