Есть API НАСА под названием NeoWs (https://api.nasa.gov/#main-content), нужно получить имя астероидов и их id

Мой интерфейс

 public interface NASAIMAGEAPI {
@GET("/planetary/apod?")
public Call<Post> getPostWithID(@Query("api_key") String KEY);

@GET("/neo/rest/v1/neo/browse?")
public Call<Comet>getJSON(@Query("api_key") String KEY);}

Мой POJO

    public class Comet {


    @SerializedName("near_earth_objects")
    @Expose
    private List<NearEarthObject> nearEarthObjects = null;



    public List<NearEarthObject> getNearEarthObjects() {
        return nearEarthObjects;
    }

    public void setNearEarthObjects(List<NearEarthObject> nearEarthObjects) {
        this.nearEarthObjects = nearEarthObjects;
    }}

Мой List NearEarthObject

    package com.example.nasapp;



import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class NearEarthObject {


    @SerializedName("id")
    @Expose
    private String id;

    @SerializedName("name")
    @Expose
    private String name;


    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }}

И наконец мой mainActivity

package com.example.nasapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    TextView textViewName;
    TextView textViewDesc;
    ImageView imageView;

    TextView textViewNameComet;
    private String KEY = "...";
    


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewName = (TextView) findViewById(R.id.textViewTitle);
        textViewDesc = (TextView) findViewById(R.id.textViewDesc);
        imageView = (ImageView) findViewById(R.id.imageView);
        textViewNameComet = (TextView) findViewById(R.id.textViewNameComet);
        loadJSON();
    }


    private void loadJSON() {

        NetworkService.getInstance()
                .getHolder()
                .getPostWithID(KEY)
                .enqueue(new Callback<Post>() {
                    @Override
                    public void onResponse(@NonNull Call<Post> call, @NonNull Response<Post> response) {
                        Post post = response.body();
                        textViewName.append(post.getTitle());
                        textViewDesc.append(post.getExplanation());
                        Picasso.with(getApplicationContext())
                                .load(post.getUrl())
                                .placeholder(R.drawable.ic_launcher_background)
                                .error(R.drawable.dino_error)
                                .into(imageView);
                    }
                    
                    @Override
                    public void onFailure(@NonNull Call<Post> call, @NonNull Throwable t) {
                        Toast toast = Toast.makeText(getApplicationContext(), "Error occurred while getting request!", Toast.LENGTH_SHORT);
                        t.printStackTrace();
                    }
                });
        NetworkService.getInstance()
                .getHolder()
                .getJSON(KEY)
                .enqueue(new Callback<Comet>() {
                    @Override
                    public void onResponse(Call<Comet> call, Response<Comet> response) {

                        Log.d("log", "вошел");
                        Comet comets = response.body();
                        
                    }

                    @Override
                    public void onFailure(Call<Comet> call, Throwable t) {
                        Log.d("log", "не вошел" + t);


                    }
                });
    }
} 

Вопросы. Правильно ли я создал POJO и list? Как отобразить id и name в данном случае?


Ответы (0 шт):