I got stuck trying Volley's synchronization process (Java)

Try synchronous processing

Based on the information that Volley can also perform synchronous processing, the following sources were executed.

app/build.gradle


dependencies {
    implementation 'com.android.volley:volley:1.1.1'
}

MyActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String url = "https://example.com/";

        RequestFuture<String> future = RequestFuture.newFuture();
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future);

        RequestQueue queue = Volley.newRequestQueue(MyActivity.this);
        queue.add(stringRequest);

        try {
            String result = future.get(10000, TimeUnit.MILLISECONDS);
            Log.d(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Time out occurs

I get a java.util.concurrent.TimeoutException. I can't see the cause because I can only tell that the exception was thrown by reading Logcat.

Execute (solve) in a non-UI thread

Since Android3.0, NetworkOnMainThreadException occurs when synchronizing process is executed in UI thread. Apparently Volley doesn't fall and times out.

When I rewrote it as follows using AsyncTask, I was able to get it normally.

MyActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] objects) {
                String url = "https://example.com/";

                RequestFuture<String> future = RequestFuture.newFuture();
                StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future);

                RequestQueue queue = Volley.newRequestQueue(MyActivity.this);
                queue.add(stringRequest);

                try {
                    String result = future.get(10000, TimeUnit.MILLISECONDS);
                    Log.d(TAG, result);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        }.execute();
    }

Recommended Posts

I got stuck trying Volley's synchronization process (Java)
I got stuck while studying Java SE 7/8 Bronze
I got stuck in File
I got stuck trying to write a where in clause in ActiveRecord
[JAVA] Project Euler, I got stuck in Q8, so make a note
[RSpec] What I got stuck when I wrote the mailer (password reset process) test
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
Where I got stuck in today's "rails tutorial" (2020/10/04)
I got stuck when port forwarding with VBox
Where I got stuck in today's "rails tutorial" (2020/10/07)
Java buffering process
Java check process
Where I got stuck with Swift Package Manager support
I got stuck in a clone of a two-dimensional array