andoid/java/Qulley/gson:获取请求API未正确返回API

发布于 2025-01-23 20:06:45 字数 5824 浏览 0 评论 0原文

我是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/code>和api/songs的请求。 URI是正确的,当我在浏览器中访问API时,我可以看到它可以正常工作,因此一定是我缺少的小问题。

当我在应用程序中单击“获取歌曲”按钮时,似乎我的ToString()函数在歌曲类中不起作用,当我单击GET Artists时,该应用程序完全崩溃了。

这是提出请求的函数:

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

最后在应用程序中的increate()函数中。

@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响应:

”在此处输入图像说明”

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2025-01-30 20:06:46

表明toString()未正确实现在您的歌曲类

...
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捕获此类错误。

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.

两相知 2025-01-30 20:06:46

我发现我的错误。我确实在ToString()函数签名中有一个错字,但是当我重命名Artistsong类中的变量时,我也可以正确返回数据就像在JSON回应中一样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文