当我单击电影中的某个项目以获取其剧情和生产年份时,我试图发送另一个请求,但这给了我一个错误。
public class MainActivity extends AppCompatActivity implements
Response.Listener < JSONObject > , Response.ErrorListener, AdapterView.OnItemClickListener {
EditText movienameText;
Button getButton;
TextView yearText, genreText, titleText, plotText, nameText;
ListView moviesList;
private ResourceBundle response;
ArrayAdapter < String > adapter;
ArrayList < String > moviesname = new
ArrayList < String > ();
ArrayList < String > moviesyear = new
ArrayList < String > ();
ArrayList < String > moviesgenre = new
ArrayList < String > ();
ArrayList < String > moviesplot = new
ArrayList < String > ();
String x, plot, genre, year;
RequestQueue
queue;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movienameText = findViewById(R.id.MovienameText);
getButton = findViewById(R.id.getButton);
moviesList = findViewById(R.id.moviesList);
moviesList.setOnItemClickListener(this);
adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, moviesname);
}
public void get(View view) {
queue = Volley.newRequestQueue(this);
String url1 = "http://www.omdbapi.com/?s=" + movienameText.getText().toString() + "&apikey=1a382b30";
JsonObjectRequest request1 = new JsonObjectRequest(Request.Method.GET, url1, null, this, this);
queue.add(request1);
}
@Override public void onResponse(JSONObject response) {
try {
for (int i = 0; i < response.getJSONArray("Search").length(); i++) {
if (i < response.getJSONArray("Search").length()) {
moviesname.add(response.getJSONArray("Search").getJSONObject(i).getString("Title"));
} else {
break;
}
}
moviesList.setAdapter(adapter);
year = response.getString("Year");
} catch (JSONException e) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
@Override public void onErrorResponse(VolleyError error) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
@Override public void onItemClick(AdapterView << ? > parent, View view, int position, long id) { //
genre = response.getString("Genre"); //
plot = response.getString("Plot");
year = response.getString("Year");
x = moviesList.getItemAtPosition(position).toString();
String url2 = "http://www.omdbapi.com/?t=" + moviesList.getItemAtPosition(position).toString().replace("
","
")+" & apikey = 1 a382b30 ";
JsonObjectRequest request2 = new JsonObjectRequest(Request.Method.GET, url2, null, this, this); queue.add(request2); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(x).create().show(); builder.setMessage(year).create().show(); // builder.setMessage(genre).create().show(); //
builder.setMessage(plot).create().show();
}
}