andoid/java/Qulley/gson:获取请求API未正确返回API
我是Android和Java世界的菜鸟,所以我不确定这里做错了什么。我有一个Artist
和song
类似这样的类:
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);
}
});
}
对代码墙表示歉意,任何帮助都将不胜感激! :)
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.
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! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表明toString()未正确实现在您的歌曲类
中
根据您看到的输出,它 错误的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
You are overriding the wrong toString method, change
ToString
totoString
and it should fix your problem.In the future use @Override to catch such errors.
我发现我的错误。我确实在ToString()函数签名中有一个错字,但是当我重命名
Artist
和song
类中的变量时,我也可以正确返回数据就像在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
andSong
class to exactly the same as in the JSON response.