learntoknow
learntoknow

Reputation: 37

Getting JSON Object from API using POST

I want to send GET Request to get data for my application but the website only has POST Request to get the JSON raw file. It said I need to put the api as HEADER but usually in retrofit I just pass it as a parameter. What is the problem here?

Upvotes: 1

Views: 59

Answers (2)

NehaK
NehaK

Reputation: 2727

Declare you APIs like this :

@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user); 

or

@GET("users/repos")
Call<List<Repo>> listRepos();

then call this like :

Call< List<Repo> > call = movieApiService.listRepos(API_KEY);
call.enqueue(new Callback< List<Repo> >() {
@Override
public void onResponse(Call< List<Repo> > call, Response< List<Repo> > response) {
List<Repo> data = response.body();
Log.d(TAG, "Number of data received: " + data.size());
}

For more info try this link https://android.jlelse.eu/consuming-rest-api-using-retrofit-library-in-android-ed47aef01ecb

Upvotes: 0

reza_khalafi
reza_khalafi

Reputation: 6544

For post request in Retrofit try code like this:

@FormUrlEncoded
@POST("CUSTOM_URL")
Call<ResponseBody> customMethodName(
  @Header("custom_header") String customHeader,
  @Field("custmom_field_as_body") int customFieldAsBody
);  

I think you have forgotten to add the annotation top of request.

Upvotes: 1

Related Questions