Retrofit2: Sending Post Request With String Request
Hi I am new to android and now I am using Retrofit for integrating the web services. I am not understanding how to send parameters to the server using Retrofit POST request. Please
Solution 1:
Make a new model class :
publicclassCredentials {
privateString email;
privateString password;
publicCredentials(String email, String password) {
this.email = email;
this.password = password;
}
}
Then modify your interface :
publicinterfacePostInterface {
@POST("User/DoctorLogin")
Call<String> getStringScalar(@Body Credentials body);
}
Then call it like this :
service.getStringScalar(newCredentials("device3@gmail.com", "1234")).enqueue(newCallback<String>() {
@OverridepublicvoidonResponse(Call<String> call, Response<String> response) {
System.out.println("result is====>" + response.body());
}
@OverridepublicvoidonFailure(Call<String> call, Throwable t) {
System.out.println("Failuere");
}
});
Solution 2:
publicinterfacePostInterface {
@POST("User/DoctorLogin")
Call<JsonObject> getStringScalar(@Field("PARAMATER_ID") String body);}
You can use string also in place of jsonobject,but i would recomment using json. "@Field(YOUR_PARAMETER)" is the way you use this
Solution 3:
Read this artical this will help you to understand retrofit.
Solution 4:
create a model class
publicclassModel{
String userName;
String password;
}
generate getter and setter
modify your interface
publicinterfaceApiInterface {
@FormUrlEncoded@POST("User/DoctorLogin")
Call<Model> loadDetail(@Field("user_id") String user_id,
@Field("password") String password);
}
and call it like this
Retrofitretrofit=newRetrofit.Builder()
.addConverterFactory(GsonConverterFactory.create()).baseUrl(url)
.build();
ApiInterfaceservice= retrofit.create(ApiInterface.class);
Call<Model> call = service.loadDetail("username", "password");
call.enqueue(newCallback<Model>() {
@OverridepublicvoidonResponse(Call<Model> call, Response<Model> response) {
intstatusCode= response.code();
Modeluser= response.body();
Log.d("userName: " user.getUserName());
Log.d("password: " + user.getPassword());
}
@OverridepublicvoidonFailure(Call<Model> call, Throwable t) {
Toast.makeText(getApplicationContext(), "data not found", Toast.LENGTH_SHORT).show();
}
Post a Comment for "Retrofit2: Sending Post Request With String Request"