简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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