我练习使用电影API创建应用。 这个想法是:
1-用户输入电影名称。
2-取这个名字并将其放在如下的链接表格中:
Constants.URL_LEFT + searchTerm + Constants.URL_RIGHT + Constants.API_KEY
这里我有两个概率:
1-输入的单词是正确的,并且匹配一个电影名称,例如(" Batman"),那么每件事情都是正确的。
2-输入字不正确,例如(" Bamtan"),然后我有错误信息 "没有搜索值"
注意:
1-我尝试使用onErrorResponse()方法,但它仅适用于noInternetConnection!
2-我尝试使用try / catch解决问题,我的应用程序停留在捕获区域!
我的问题是: 我怎么能处理错误的输入(错误的URL)?
这是我的代码:
public List<Movie> getMovies(final String searchTerm){
movieList.clear();
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, Constants.URL_LEFT + searchTerm + Constants.URL_RIGHT + Constants.API_KEY,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray moviesArray = response.getJSONArray("Search");
for (int i = 0 ; i < moviesArray.length() ; i++){
JSONObject movieObj = moviesArray.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(movieObj.getString("Title"));
movie.setYear(" Released on : " + movieObj.getString("Year"));
movie.setMovieType("Type : " + movieObj.getString("Type"));
movie.setPoster(movieObj.getString("Poster"));
movie.setImdbId(movieObj.getString("imdbID"));
movieList.add(movie);
}
// notify the adapter for changes! very important...
movieRecyclerViewAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
// app just save the exception and repeat it after that for correct values also!
if (e.getMessage().equals("No value for Search")){
Toast.makeText(MainActivity.this, "Can not find any movie with this name!", Toast.LENGTH_SHORT).show();
MainActivity.this.onRestart();
popupDialog();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// dealing with losing network connection
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("Error Found!")
.setMessage("Make Sure you have a network Connection!")
.setCancelable(false);
alertDialogBuilder.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// re-check the internet connection!
getMovies(searchTerm);
}
})
.setNegativeButton("Exit!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// close the App!
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
queue.add(objectRequest);
return movieList;
}
非常感谢提前!
答案 0 :(得分:0)
在JSONArray moviesArray = response.getJSONArray("Search");
之后检查moviesArray是否为空,如果它的null表示未找到搜索并且它在json响应中不存在,并且您可以显示您的消息,说明找不到电影。
以下是更改的外观
JSONArray moviesArray = response.getJSONArray("Search");
if(moviesArray==null){
Toast.makeText(MainActivity.this, "Can not find any movie with this name!", Toast.LENGTH_SHORT).show();
}
else
{
for (int i = 0 ; i < moviesArray.length() ; i++){
JSONObject movieObj = moviesArray.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(movieObj.getString("Title"));
movie.setYear(" Released on : " + movieObj.getString("Year"));
movie.setMovieType("Type : " + movieObj.getString("Type"));
movie.setPoster(movieObj.getString("Poster"));
movie.setImdbId(movieObj.getString("imdbID"));
movieList.add(movie);
}
// notify the adapter for changes! very important...
movieRecyclerViewAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
答案 1 :(得分:0)
首先,感谢@Psypher和@GautamSurani的帮助。
问题由:
修复1-删除HAXM的新更新并安装旧版本并创建新的虚拟设备。
2-更新if(moviesArray==null)
我上面写的if (!response.has("Search"))
响应原因始终有响应!
我的想法
模拟器保存SharedPreferences的最后一个错误值(由用户输入),即使用户输入了正确的错误值也保持打开状态。