问题
All.
I am trying to use Retrofit to send raw json as a body in a POST Request to the server.
@POST("api/apps")
Call<List<GetApps>> getApp(@Body GetApps body);
I have built the model using jsontopojo for the responses that are coming back.
Inside my call, I have put the constructors and the getters and setters and also toString().
This is my retrofit call.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
Call<List<GetApps>> call = apiService.getApp();
call.enqueue(new Callback<List<GetApps>>() {
@Override
public void onResponse(Call<List<GetApps>> call, Response<List<GetApps>> response) {
List<GetApps> GetApps2 = response.body();
Toast.makeText(getApplicationContext(),"success", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<GetApps>> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Failure", Toast.LENGTH_SHORT).show();
}
});
I am getting an error on this line :
Call<List<GetApps>> call = apiService.getApp();
It says getApp(getApps) cannot be applied to (); Not sure what should go into the ();
回答1:
You must pass GetApps
object as parameter:
GetApps getApps = new GetApps();
//set all your data on getApps
//getApps.setYourData(yourData);
Call<List<GetApps>> call = apiService.getApp(getApps);
回答2:
You make request
@POST("api/apps")
Call<List<GetApps>> getApp(@Body GetApps body);
which will accept body that will append to URL.
just add body in getApp(@Body GetApps body);
// accept JSON string as a parameter.
Happy coding!!
回答3:
At the top of Post just add @FormEncodedUrl
and pass a GetApp model as body ...
Call<List<GetApps>> call = apiservice.getApp(getAppOne);
来源:https://stackoverflow.com/questions/48055813/capturing-list-response-for-a-retrofit-call-with-json-body