首页 > 解决方案 > OmdB API 未在我的 TextViews 中显示结果

问题描述

我是 Android 开发的新手,并且已经开始使用一些小部件等。我已经到了必须将 OmDB API 数据显示到我的 Activity 的 TextViews 和 ImageView 中的地步。我正在使用 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 中什么也没有出现。我重新检查了代码并一遍又一遍地遵循同样的事情,在 Toasts 中没有显示任何内容。

我肯定找到了一些相关的 OmDB 代码,但无法帮助我。

标签: androidjson

解决方案


推荐阅读