Consulta GET con json – Retrofit 2.0
¿Cómo hacer esta consulta que voy a mencionar a continuación?
Método @GET
. La consulta debe tener este aspecto:
- Retrofit 2 - obtiene error-objeto JSON (POJO pocos para la misma solicitud)
- Solicitudes HTTP periódicas con RxJava y Retrofit
- Retrofit 2: responseBodyConverter convierte a objeto nulo
- Broken Pipe al intentar cargar una actualización de archivo grande 2
- Android: pasar dinámicamente la clase del modelo a retroalimentar la devolución de llamada
/top40?data={"ranking":"world"}
/top40?data={"ranking":"country"}
@GET("/api/top40") Call<FamousTop40Model> getStatus( // what should be there? ); class Factory { private static FamousTop40Api service; public static FamousTop40Api getInstance() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(ApiConstants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); service = retrofit.create(FamousTop40Api.class); return service; } }
¿Pueden ayudarme chicos?
EDIT: + Debería tener accessKey
en el encabezado.
- Publicar campos en este formato con Retrofit
- Serializar parámetro de consulta en Retrofit
- Iniciar sesión a través de servicio web de descanso de android utilizando retroalimentación no funciona
- Retrofit ETAG y almacenamiento en caché
- Robolectric + OkHttp + retrofit + prueba de unidad rxJava
- ¿Cómo estructurar mi aplicación usando MVP con rxjava y retrofit para obtener datos de Observables?
- RxAndroid & Retrofit Mejor aproximación para el manejo de errores
- Android: subir imagen a Twitter utilizando REST API y Fabric, error de tubería roto
3 Solutions collect form web for “Consulta GET con json – Retrofit 2.0”
Eso me ayuda:
public interface FamousTop40Api { @GET("/api/top40") Call<FamousTop40Model> getStatus( @Query("data") String ranking ); class Factory { private static FamousTop40Api service; public static FamousTop40Api getInstance() { OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); Request request = original.newBuilder() .header("accessKey", MainActivity.ACCESS_KEY) .method(original.method(), original.body()) .build(); return chain.proceed(request); } }); OkHttpClient client = httpClient.build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ApiConstants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); service = retrofit.create(FamousTop40Api.class); return service; } } }
Así que necesitas agregar @Query
y accessKey
en la cabecera con OkHttpClient
y
FamousTop40Api.Factory.getInstance().getStatus("{\"ranking\":\"country\"}").enqueue();
Puede llamar a su método GET de esta manera:
public interface EndPointInterface{ @GET(ProximityConstants.URL_FETCH_STORE_INFO) Call<Store> getStoreInfoByBeaconUUID(@Query(ProximityConstants.BEACON_UUID) String beaconUUID); }
Para llamar al método GET webservice proceda de la siguiente manera:
Call<StoreSettings> call = apiService.getStoreSettings(mStore.getStoreID(), mCustomer.getCustId(), mStore.getBeaconID(), ProximityConstants.SETTINGS_CUST_TYPE, ProximityConstants.ACTION_STORE_VISIT); call.enqueue(new Callback<StoreSettings>() { @Override public void onResponse(Call<StoreSettings> call, Response<StoreSettings> response) { ProximityUtil.writeLog(TAG, "Received store settings"); mStoreSettings = response.body(); SharedPreferences.Editor editor = mAppPreferences.edit(); editor.putString(ProximityConstants.SETTINGS_OBJ_STORE_SETTINGS, new Gson().toJson(mStoreSettings)); editor.commit(); Intent intent = new Intent(BeaconScanService.this, ProximityWelcomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); BeaconScanService.this.startActivity(intent); } @Override public void onFailure(Call<StoreSettings> call, Throwable t) { Toast.makeText(BeaconScanService.this, "Error: " + t.getMessage(), Toast.LENGTH_LONG).show(); } });
Intente lo siguiente: Create Class Task.java:
public class Task { private String ranking; public Task(String ranking) { this.ranking = ranking; } }
Y haz lo siguiente:
public interface TaskService { @POST("/top40?data=") Call<Top40Model> getStatus(@Body Task task); }
Y utilice como abajo:
Task task = new Task("world"); Task task = new Task("country"); Call<Top40Model> call=new Factory.getInstance().getStatus(task); call.enqueue(new Callback<Top40Model>() {});
- Los elementos de pestaña inflados en Tab Layout no coinciden con parent en android
- Resaltado de inspección de código: no se puede establecer diferentes estilos de resaltado para las severidades de información y de advertencia débil