Andoid/Java/凌空/GSON:对API的GET请求未正确返回

b4lqfgs4  于 2022-11-06  发布在  Java
关注(0)|答案(2)|浏览(168)

我对android和java世界还很陌生,所以我不太确定我在这里做错了什么。我有一个ArtistSong类,如下所示:
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;
} }

在我的MainActivity.java文件中,我给了一个函数来获取api/artistsapi/songs的请求。URI是正确的,当我在浏览器中访问API时,我可以看到它工作得很好,所以这一定是我遗漏的一个小问题。
当我在应用程序中单击“获取歌曲”按钮时,Song类中的toString()函数似乎不起作用,而当我单击“获取艺术家”按钮时,应用程序完全崩溃。

以下是发出请求的函数:

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

最后是应用程序中的onCreate()函数。

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

为代码墙道歉,任何帮助都是感激的!:)
另外,下面是来自浏览器内部的api响应:

yvfmudvl

yvfmudvl1#

根据您所看到的输出,它显示由于正在调用Object.toString(),因此在Song类中未正确实现toString(),而您的情况就是这样:
Song.class

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

您覆盖了错误的toString方法,请将ToString更改为toString,这样应该可以解决您的问题。
以后可以使用@Override来捕捉此类错误。

t9aqgxwy

t9aqgxwy2#

我发现了我的错误。我确实在toString()函数签名中有一个打字错误,但是当我将ArtistSong类中的变量重命名为与JSON响应中的变量完全相同时,我也可以正确地返回数据。

相关问题