2017-10-05 7 views
0

私は以下のコードを呼び出してxmlを受け取ります。StringRequestとRequestFutureを使用して、ボレーのブロッキング同期を呼び出す

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

問題はアクティビティから呼び出されたときにうまく動作しますが、IntentServiceからVolleyを使用したいのです。 intentServiceは作業が完了した後に自動的に破棄されるため、Volleyのコールバックは決してレスポンスを取得しません。

私が見つけた1つの解決策は、RequestFutureを使用し、スレッドをブロックするために.get()を呼び出すことです。私はここで私が見つけた例があります。

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 
     } 

xmlを返すサーバーとしてJSONを使用したくありません。私はStringRequestクラスを見てきましたが、私はRequestFutureをサポートするものは何も見えません。

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

おかげ

答えて

0

私はリクエストのこのタイプのためのレトロフィットを使用RequestFuture

のブロッキング機能を使用してXMLを返すようにStringRequestコードを使用するためにとにかくがあります。それは非常に使いやすく、あなたは両方のタイプのリクエスト(同期と非同期)を行うことができます http://square.github.io/retrofit/

+0

こんにちは、私はVolleyでできないことを望みますか? AndroidがVolleyを採用し、IntentServiceから使用することができないというのは奇妙なことです。私はRetrofitを見ていきますが、私はVolleyですべての可能性を使い果たしたい – turtleboy

関連する問題