我是Android开发的新手,并且从一些小部件等开始了新的学习。我到达了必须在活动的TextViews和ImageView中显示OmDB API数据的地步。我正在使用JSON将数据显示到TextViews中。
这是我的XML代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/etMovieName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Movie Name"
android:inputType="textPersonName"
/>
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
/>
</LinearLayout>
<TextView
android:id="@+id/tvMovieYear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Year"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tvMovieActor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Actors"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tvMovieDirector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Directors"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tvMovieCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Countries"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tvMovieLanguage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Languages"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tvMoviePlot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PLot"
android:textSize="25sp"
/>
<ImageView
android:id="@+id/imgPoster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_round" />
这是我对小部件的引用:
EditText edtMovieName;
Button btnSearchMovie;
TextView tvYear, tvActor, tvDirector, tvCountry, tvLanguage, tvPlot;
ImageView ivPoster;
然后我有OnCreate和我的按钮单击事件代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies_database);
edtMovieName = findViewById(R.id.etMovieName);
btnSearchMovie = findViewById(R.id.btnSearch);
tvYear = findViewById(R.id.tvMovieYear);
tvActor = findViewById(R.id.tvMovieActor);
tvDirector = findViewById(R.id.tvMovieDirector);
tvCountry = findViewById(R.id.tvMovieCountry);
tvLanguage = findViewById(R.id.tvMovieLanguage);
tvPlot = findViewById(R.id.tvMoviePlot);
ivPoster = findViewById(R.id.imgPoster);
btnSearchMovie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String movieName = edtMovieName.getText().toString();
if(movieName.isEmpty())
{
edtMovieName.setError("Please Enter Movie Name");
return;
}
String url = "http://www.omdbapi.com/?t=" + movieName + "&plot=full&apikey=e03663ba";
RequestQueue RQ = Volley.newRequestQueue(MoviesDatabaseActivity.this);
StringRequest strReq = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
String strMovieYear = jObj.getString(response);
String tru = "True";
if(strMovieYear.equals(tru))
{
Toast.makeText(getApplicationContext(), "Found", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "NOT Found", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
RQ.add(strReq);
}
});
}
}
问题
此代码的问题是,当我运行该应用程序时,按了搜索按钮后,吐司中什么也没出现。我重新检查了代码,然后一遍又一遍地重复同样的事情,在Toasts中什么也没显示。
我当然已经找到了一些相关的OmDB代码,但无济于事。