Home > Software engineering >  Andoid/Java/Volley/GSON: GET request to API not returning correctly
Andoid/Java/Volley/GSON: GET request to API not returning correctly

Time:04-26

I'm quite a noob to the android and java world so I'm not quite sure what I'm doing wrong here. I have an Artist and Song class like this:

Artist.java

public class Artist {
private int ID;

private String Name;

private String Bio;

private int YearFormed;

@Override
public String toString()
{
    return Name;
} }

Song.java

public class Song {
private int ID;
private String Name;
private String Length;
private String Genre;
private String Lyrics;

private int ArtistID;

public String printName()
{
    return Name;
}

public String ToString()
{
    String toReturn = "ID: "   Integer.toString(ID)   "\nArtist: "   Integer.toString(ArtistID)
             "\nLength: "   Length   "\nGenre: "   Genre;
    return toReturn;
} }

In my MainActivity.java file I gave a function to GET request to api/artists and api/songs. The URI is correct and when I visit the API in the browser I can see that it works fine, so it must be a small issue that I'm missing.

When I click the Get Songs button in my app, it seems as though my toString() function in the Song class isn't working, and when I click the Get Artists button the app crashes completely.

enter image description here

Here is the functions to make the requests:

 public void getArtists(View v)
{
    final TextView outputTextView = (TextView) findViewById(R.id.outputTextView);

    try
    {
        RequestQueue queue = Volley.newRequestQueue(this);
        Log.d(TAG, "Making request to /api/artists/");
        try
        {
            StringRequest strObjRequest = new StringRequest(Request.Method.GET, SERVICE_URI   "/artists",
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response)
                        {
                            Gson gson = new Gson();
                            Artist[] artists = gson.fromJson(response, Artist[].class);
                            for(Artist a: artists)
                            {
                                outputTextView.setText(a.toString());
                                Log.d(TAG, "Data: "   a.toString());

                            }
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void one rrorResponse(VolleyError error)
                        {
                            outputTextView.setText(error.toString());
                            Log.d(TAG, "Error"   error.toString());
                        }
                    });
            queue.add(strObjRequest);
        }
        catch (Exception e2)
        {
            Log.d(TAG, e2.toString());
            outputTextView.setText(e2.toString());
        }
    }
    catch (Exception e1)
    {
        Log.d(TAG, e1.toString());
        outputTextView.setText(e1.toString());
    }
}

public void getSongs(View v)
{
    final TextView outputTextView = (TextView) findViewById(R.id.outputTextView);

    try
    {
        RequestQueue queue = Volley.newRequestQueue(this);
        Log.d(TAG, "Making request to /api/songs/");
        try
        {
            StringRequest strObjRequest = new StringRequest(Request.Method.GET, SERVICE_URI   "/songs",
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response) {
                            Gson gson = new Gson();
                            Song[] songs = gson.fromJson(response, Song[].class);
                            for (Song s : songs) {
                                outputTextView.append("\n"   s.toString());
                                Log.d(TAG, "Data: "   s.toString());

                            }
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void one rrorResponse(VolleyError error)
                        {
                            outputTextView.setText(error.toString());
                            Log.d(TAG, "Error"   error.toString());
                        }
                    });
            queue.add(strObjRequest);

        }
        catch (Exception e2)
        {
            Log.d(TAG, e2.toString());
            outputTextView.setText(e2.toString());
        }
    }
    catch (Exception e1)
    {
        Log.d(TAG, e1.toString());
        outputTextView.setText(e1.toString());
    }
}

And finally in the onCreate() function in the app.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Button getArtistButton = (Button) findViewById(R.id.getArtistsButton);
    getArtistButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            getArtists(view);
        }
    });

    Button getSongsButton = (Button) findViewById(R.id.getSongsButton);
    getSongsButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            getSongs(view);
        }
    });
}

Apologies for the walls of code, any help is appreciated! :)

Also, here is the api response from inside the browser: enter image description here

enter image description here

CodePudding user response:

Based on the output you're seeing, it shows that the toString() is not implemented correctly in your Song class since the Object.toString() is being called, and that is the case for you:

Song.class

...
public String ToString()  //Should be toString()
{
    String toReturn = "ID: "   Integer.toString(ID)   "\nArtist: "   Integer.toString(ArtistID)
             "\nLength: "   Length   "\nGenre: "   Genre;
    return toReturn;
}

You are overriding the wrong toString method, change ToString to toString and it should fix your problem.

In the future use @Override to catch such errors.

CodePudding user response:

I found my error. I did have a typo in the toString() function signature, but also I could return the data correctly when I renamed the variables in the Artist and Song class to exactly the same as in the JSON response.

  • Related