2016-09-27 4 views
0

A VolleyRequest не обрабатывается должным образом в пределах Service.Служба, пытающаяся выполнить запрос с использованием библиотеки Volley, но обратный вызов никогда не называется

public class StatusService extends Service { 
    private static final String TAG = StatusService.class.getSimpleName(); 

    @Override 
    public void onStart(Intent intent, int startId) { 
    // check the global background data setting 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    if (!cm.getBackgroundDataSetting()) { 
     stopSelf(); 
     return; 
    } 

    RequestQueue mRequestQueue; 
    { // setup mRequestQueue 
     // Instantiate the cache 
     Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap 

     // Set up the network to use HttpURLConnection as the HTTP client. 
     Network network = new BasicNetwork(new HurlStack()); 

     // Instantiate the RequestQueue with the cache and network. 
     mRequestQueue = new RequestQueue(cache, network); 

     // Start the queue 
     mRequestQueue.start(); 
    } 

    // do the actual work, in a separate thread 
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "http://mydomain/something", null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
     // I am sure the server side is working properly 
     Log.d(TAG, "This never gets called! -> On response: " + response); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
     Log.e(TAG, "error", error); 
     } 
    }); 
    mRequestQueue.add(request); 
    Log.d(TAG, "This will be logged successfully"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
    return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    handleIntent(intent); 
    return START_NOT_STICKY; 
    } 
} 

Запрос отправляется, но предоставленное действие обратного вызова не вызывается. Когда сервер выключен, обратный вызов ошибки вызывается с TimeOutError. Это проблема потока? Если да, что может быть неправильным?

ответ

0

Не слишком уверен, почему можно было бы назвать, а не другого. Однако, согласно документации, хотя это может быть совершенно не важно для вашей проблемы, обратные вызовы вызываются в основном потоке.

+0

Я не могу понять, почему это не работает! – FtheBuilder