Send Post Request with params using Retrofit
I have found the solution. The issue was a problem in my classes structure. So i updated them like the following samples.
public class LandingPageReport {
private ArrayList<LandingPageReportItem> GetDetailWithMonthWithCodeResult;
// + Getter Setter methods
}
public class LandingPageReportItem {
private String code;
private String field1;
// + Getter Setter methods
}
And then i use this retrofit configuration
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
@Field("monthact") String monthact,
Callback<LandingPageReport> cb);
How to pass parameters to POST request using Retrofit and then serialize request?
You should use @Body
annotation:
Use this annotation on a service method param when you want to directly control the request body of a POST/PUT request... The object will be serialized using the Retrofit instance Converter and the result will be set directly as the request body. Body parameters may not be null.
You might use
@POST("/startup")
fun getDataVenom(
@Body body: BodyDTO
): Call<ObjectFromResponse>
where BodyDTO
is
data class BodyDTO(
@SerializedName("players") private val players: Array<String>,
@SerializedName("action") private val action: String
)
Sending POST Request With Parameters Using Retrofit
Use this model instead
public class MyPojo
{
private PaymentInfo paymentInfo;
private String status;
private String orderHash;
public PaymentInfo getPaymentInfo ()
{
return paymentInfo;
}
public void setPaymentInfo (PaymentInfo paymentInfo)
{
this.paymentInfo = paymentInfo;
}
public String getStatus ()
{
return status;
}
public void setStatus (String status)
{
this.status = status;
}
public String getOrderHash ()
{
return orderHash;
}
public void setOrderHash (String orderHash)
{
this.orderHash = orderHash;
}
@Override
public String toString()
{
return "ClassPojo [paymentInfo = "+paymentInfo+", status = "+status+", orderHash = "+orderHash+"]";
}
}
Payment modal
public class PaymentInfo
{
private String url;
public String getUrl ()
{
return url;
}
public void setUrl (String url)
{
this.url = url;
}
@Override
public String toString()
{
return "ClassPojo [url = "+url+"]";
}
}
Retrofit Post Parameter
Try using this
public interface SafeUserApi {
@FormUrlEncoded
@POST("/api/userlogin")
void getUserLogin(
@Field("client_id") String id,
@Field("client_secret") String secret,
@Field("username") String uname,
@Field("password") String password,
Callback<LoginResult> cb
);
}
Here parm1 is the POST parameter that you will be passing it to the server.
This will solve your problem
in case if you are using PHP u can access the param1 using $uname= $_POST('username');
EDIT 1:
retrofit 2.0 version:
public interface SafeUserApi {
@FormUrlEncoded
@POST("/api/userlogin")
Call<ResponseBody> getUserLogin(
@Field("client_id") String id,
@Field("client_secret") String secret,
@Field("username") String uname,
@Field("password") String password
);
}
Send POST parameters with Retrofit
There are some info which you know about Retrofit ....
Your BASE_URL must be end with / .
When you using
@Field
notation you must put@FormUrlEncoded
in Your Api call.When you using {user} in the API method you have to use
@Path("user") String user
to relate to url data .Your POST method URL will be like this
@POST("users/{user}")
.When your response Callback done the actual Data inside your
Response<GitHubEmail> response
in this variable. You have to useresponse.body()
to get what you get response from API CALL.
Here is a sample code
@FormUrlEncoded
@POST("users/{user}")
Call<YourResultPojoClassHere> yourFuntionName(@Field("id") String id,@Path("user") String path);
please take look on below code ....
callEmail.enqueue(new Callback<GitHubEmail>() {
@Override
public void onResponse(Call<GitHubEmail> call, Response<GitHubEmail> response) {
if (response.isSuccessful()) {
if (response.body().getSuccess())
Toast.makeText(ClassName.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
else
Toast.makeText(ClassName.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
} else
Toast.makeText(ClassName.this, "Sorry for inconvince server is down", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<GitHubEmail> call, Throwable t) {
Toast.makeText(ClassName.this, "Check your Internet connection", Toast.LENGTH_SHORT).show();
}
}
});
Related Topics
Oauth 2.0 Authorization for Linkedin in Android
How to Get My Location Changed Event with Google Maps Android API V2
Editing Functionality of Host Card Emulation in Android
What Is a Full Android Database Helper Class for an Existing SQLite Database
Relativelayout Is Taking Fullscreen for Wrap_Content
Cordova 3.5.0 Install Error- Please Install Android Target 19
Android: How to Get a File's Creation Date
Android Scale Animation on View
[Install_Failed_No_Matching_Abis: Failed to Extract Native Libraries, Res=-113]
Why Does It Take So Long for Android's Mediaplayer to Prepare Some Live Streams for Playback
Android Singleton with Global Context
Android Action Bar Searchview as Autocomplete
How to Install Android Market App on the Emulator
How to Get the Mobile Number of Current Sim Card in Real Device
Get Battery Level Before Broadcast Receiver Responds for Intent.Action_Battery_Changed