2016-07-05 18 views
4

Wie mache ich diese Abfrage, die ich unten erwähnen werde?GET Abfrage mit JSON - Retrofit 2.0

Methode @GET. Abfrage sollte wie folgt aussieht:

/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; 
     } 
    } 

Könnt ihr mir helfen?

EDIT: + Ich sollte accessKey in der Kopfzeile haben.

+0

Sie möchten eine Get or Put-Anfrage stellen? – Drv

+0

Anfrage erhalten, aber in der empfangenen API wird gesagt, dass ich JSON senden sollte, um Daten zu empfangen. Es ist sogar möglich, jsons params in Get zu senden? – y07k2

+0

Sie können die folgende Antwort folgen: @ y07k2 – Lampard

Antwort

0

Sie können Ihre GET-Methode wie folgt aufrufen:

public interface EndPointInterface{ 
@GET(ProximityConstants.URL_FETCH_STORE_INFO) 
    Call<Store> getStoreInfoByBeaconUUID(@Query(ProximityConstants.BEACON_UUID) String beaconUUID); 

} 

Um die GET-Methode Webservice Aufruf wie folgt vorgehen:

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(); 
      } 
     }); 
+0

'@Query (" ranking ") String ranking wird diese Anfrage mit'? Daten erstellen = {...} '? Wo kann ich Header hinzufügen? – y07k2

+0

haben Sie nicht bekommen, in Ihrem Service erhalten Sie Pass-Parameter oder Header? – Lampard

+0

Ich habe es nicht verstanden. 'accessKey' in der Kopfzeile und' data' json, die den 'rank' String-Wert in einem Parameter enthält. – y07k2

0

wie unten Versuchen: Erstellen Klasse Task.java:

public class Task { 
private String ranking; 

public Task(String ranking) { 
    this.ranking = ranking; 
} 
} 

Und tun wie folgt:

public interface TaskService { 

@POST("/top40?data=") 
Call<Top40Model> getStatus(@Body Task task); 

} 

Und wie unten verwenden:

Task task = new Task("world"); 
Task task = new Task("country"); 

Call<Top40Model> call=new Factory.getInstance().getStatus(task); 
call.enqueue(new Callback<Top40Model>() {}); 
+0

'" data = {"ranking": "{ranking}"} "Wie kann ich' ranking 'durch einen String-Wert ersetzen? weil z.B. Diese URL ist gültig in Postman 'http://5.135.147.222:8400/api/top40?data= {" Ranking ":" world "}'. Beachten Sie, dass '{..}' alle Parameter enthält. – y07k2

+1

Fehler erhalten: 'IllegalArgumentException: URL-Abfragezeichenfolge 'data = {" ranking ":" {ranking} "}" darf keinen replace-Block haben. Verwenden Sie für dynamische Abfrageparameter @ Query. bei Verwendung von @Query. Ich finde Klammern, die da sein müssen ... – y07k2

+0

Er sagte GET, nicht POST. – nasch

2

Das hilft mir:

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

So müssen Sie @Query und accessKey in Header hinzufügen mit OkHttpClient

und

FamousTop40Api.Factory.getInstance().getStatus("{\"ranking\":\"country\"}").enqueue();