问题
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 help me. Thanks.
MainActivity:-
String url = "XXXXXXXXX/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).
addConverterFactory(GsonConverterFactory.create()).build();
PostInterface service = retrofit.create(PostInterface.class);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("email", "device3@gmail.com");
jsonObject.put("password", "1234");
} catch (JSONException e) {
e.printStackTrace();
}
final String result = jsonObject.toString();
service.getStringScalar(result).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
System.out.println("result is====>" + response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
System.out.println("Failuere");
}
});
}
PostInterface:-
public interface PostInterface {
@POST("User/DoctorLogin")
Call<String> getStringScalar(@Body String body);
}
回答1:
Make a new model class :
public class Credentials {
private String email;
private String password;
public Credentials(String email, String password) {
this.email = email;
this.password = password;
}
}
Then modify your interface :
public interface PostInterface {
@POST("User/DoctorLogin")
Call<String> getStringScalar(@Body Credentials body);
}
Then call it like this :
service.getStringScalar(new Credentials("device3@gmail.com", "1234")).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
System.out.println("result is====>" + response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
System.out.println("Failuere");
}
});
回答2:
public interface PostInterface {
@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
回答3:
Read this artical this will help you to understand retrofit.
回答4:
create a model class
public class Model {
String userName;
String password;
}
generate getter and setter
modify your interface
public interface ApiInterface {
@FormUrlEncoded
@POST("User/DoctorLogin")
Call<Model> loadDetail(@Field("user_id") String user_id,
@Field("password") String password);
}
and call it like this
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create()).baseUrl(url)
.build();
ApiInterface service = retrofit.create(ApiInterface.class);
Call<Model> call = service.loadDetail("username", "password");
call.enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
int statusCode = response.code();
Model user = response.body();
Log.d("userName: " user.getUserName());
Log.d("password: " + user.getPassword());
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
Toast.makeText(getApplicationContext(), "data not found", Toast.LENGTH_SHORT).show();
}
来源:https://stackoverflow.com/questions/40816733/retrofit2-sending-post-request-with-string-request