简体   繁体   English

我的应用程序在尝试使用 Volley 时崩溃

[英]My application crashes while trying to use Volley

I try to capture data using a volley, but my application crashes.我尝试使用截击来捕获数据,但我的应用程序崩溃了。 the application opens but my application crashes when I press the search button.应用程序打开,但当我按下搜索按钮时我的应用程序崩溃。

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);

}

my application crashes right here我的应用程序在这里崩溃

      tvYear.setText(result);

my application doesn't crash when I delete this line.删除此行时,我的应用程序不会崩溃。 I want to see the value of requestin but it crashes我想查看 requestin 的值,但它崩溃了

You can pritnt log aftr every line to see the variable's value.您可以在每一行之后打印日志以查看变量的值。 Please point out what exact the log cat show error?请指出究竟是什么日志猫显示错误?

Try using a JsonObjectRequest instead.尝试改用JsonObjectRequest

You won't have to do the potentially bug causing conversion.您不必执行导致转换的潜在错误。 If anything goes wrong in the conversion process and objects are mapped incorrectly, you get the issue you're trying to fix.如果转换过程中出现任何问题并且对象映射不正确,那么您就会遇到要解决的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM