Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
¿Cómo puedo devolver datos en el método de Retrofit onResponse?

Soy nuevo con la actualización y quiero hacer que mi método getData devuelva un objeto de función. Cual es la forma mas fácil de hacer eso?

Servicio de datos.java

 public class DataService { private static final String TAG = MainActivity.class.getSimpleName(); private final ApiClient apiClient; public DataService() { apiClient = new ApiClientFactory().createApiClient(); } public List<Feature> getData(){ apiClient.getData().enqueue(new Callback<DataResponse>() { @Override public void onResponse(Call<DataResponse> call, Response<DataResponse> response) { List<Feature> features = response.body().getFeatures(); Log.d(TAG, "Data successfully downloaded"); } @Override public void onFailure(Call<DataResponse> call, Throwable t) { Log.e(TAG, t.toString()); } }); //I need to return features in getData method } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No puede return , debe "devolver la llamada".

Extraiga esa clase de devolución de llamada interna a un parámetro.

 public void getData(Callback<DataResponse> callback){ apiClient.getData().enqueue(callback); }

En tu otro código

 // DataService service = ...; // Define Callback Callback<DataResponse> responseCallback = new Callback<DataResponse>() { @Override public void onResponse(Call<DataResponse> call, Response<DataResponse> response) { List<Feature> features = response.body().getFeatures(); Log.d(TAG, "Data successfully downloaded"); // Data is returned here for (Feature f: features) { Log.d("feature", String.valueOf(f)); // for example } } @Override public void onFailure(Call<DataResponse> call, Throwable t) { Log.e(TAG, t.toString()); } }; // Call it service.getData(responseCallback);

También puede hacer service.getData(new Callback<DataResponse>() { ... });

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!