2

I have the following code which works to make a call and receives xml.

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            // Do something with the response
                            Log.e(TAG, "response from RRSendCarerLocation = " + response);

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // Handle error
                            Log.e(TAG, "error: RRSendCarerLocation = " + error);



                        }
                    });


            rq.add(stringRequest);

.

The problem i have is, this works fine when called from an Activity but i want to use Volley from an IntentService. An intentService destroys itself after the work is done, so the Volley callbacks never retreive the response.

one solution i have found would be to use RequestFuture and call .get() to block the thread. i have an example below that i found here.

Can I do a synchronous request with volley?

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(URL, new JSONObject(), future, future);
requestQueue.add(request);

try {
            return future.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // exception handling
        } catch (ExecutionException e) {
            // exception handling
        } catch (TimeoutException e) {
            // exception handling
        }

.

I don't want to use JSON as the server returns xml. i've looked at the StringRequest class but i cannot see anything that supports a RequestFuture.

http://griosf.github.io/android-volley/com/android/volley/toolbox/StringRequest.html

Is there anyway to use the StringRequest code to return xml using the blocking functionality of RequestFuture

thanks

turtleboy
  • 8,210
  • 27
  • 100
  • 199

2 Answers2

1

This is in case you did not find an optimum solution or for any people who come across this post with the same question. If you want to use StringRequest with FutureRequest you can try out the solution below.

// Setup a RequestFuture object with expected return/request type 
RequestFuture<String> future = RequestFuture.newFuture();

// organize your string request object
StringRequest request = new StringRequest(url, future, future);

// add request to queue
VolleySingleton.getInstance(context).addToRequestQueue(request);

try {
    // Set an interval for the request to timeout. This will block the
    // worker thread and force it to wait for a response for 60 seconds
    // before timing out and raising an exception
    String response = future.get(60, TimeUnit.SECONDS);

    // Do something with the response
    Log.e(TAG, "response from RRSendCarerLocation = " + response);
    return Result.success();
} catch (InterruptedException | TimeoutException | ExecutionException e) {
    e.printStackTrace();
    return Result.retry();
}
Douglas Hosea
  • 1,002
  • 13
  • 30
0

I use Retrofit for this types of requests. It is very easy to use and allows you to make both types of requests (sync and unsync) http://square.github.io/retrofit/

asanchezyu
  • 680
  • 9
  • 11
  • Hi, so what i want cannot be done in Volley? Seems strange that Android have adopted Volley and cannot be used from an IntentService. I'll have a look at Retrofit but i want to exhaust all possiblities with Volley – turtleboy Oct 05 '17 at 15:38