簡體   English   中英

我的應用程序在嘗試使用 Volley 時崩潰

[英]My application crashes while trying to use Volley

我嘗試使用截擊來捕獲數據,但我的應用程序崩潰了。 應用程序打開,但當我按下搜索按鈕時我的應用程序崩潰。

public class MainActivity extends AppCompatActivity {

TextView tvYear,tvDirector,tvActor,tvLanguage,tvPlots,tvCountry;
ImageView ivPoster;
EditText edName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edName     = findViewById(R.id.edName);
    tvYear     = findViewById(R.id.tvYear);
}

public void search(View view) {
    String mName = edName.getText().toString();
    if(mName.isEmpty())
    {
        edName.setError("please provide movie name");
        return;
    }
    String url = "HTTP://www.omdbapi.com/?t="+mName+"&plot=full&apikey=myApiKey";

    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject movie = new JSONObject(response);

                        String result = movie.getString("Response");
right there  crash app---->     tvYear.setText(result);
                        if(result.equals("True"))
                        {

                            Toast.makeText(MainActivity.this, "Found", Toast.LENGTH_SHORT).show();


                            if(posterUrl.equals("N/A"))
                            {

                            }
                            else
                            {

                            }
                        }
                        else
                        {

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
    queue.add(request);

}

我的應用程序在這里崩潰

      tvYear.setText(result);

刪除此行時,我的應用程序不會崩潰。 我想查看 requestin 的值,但它崩潰了

您可以在每一行之后打印日志以查看變量的值。 請指出究竟是什么日志貓顯示錯誤?

嘗試改用JsonObjectRequest

您不必執行導致轉換的潛在錯誤。 如果轉換過程中出現任何問題並且對象映射不正確,那么您就會遇到要解決的問題。

https://developer.android.com/training/volley/request

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM