Send Post Request with Params Using Retrofit

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 ....

  1. Your BASE_URL must be end with / .

  2. When you using @Field notation you must put @FormUrlEncoded in Your Api call.

  3. When you using {user} in the API method you have to use @Path("user") String user to relate to url data .

  4. Your POST method URL will be like this @POST("users/{user}").

  5. When your response Callback done the actual Data inside your Response<GitHubEmail> response in this variable. You have to use response.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



Leave a reply



Submit