gpt4 book ai didi

android - 使用 Retrofit2 在 POST 请求中发送 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:57 32 4
gpt4 key购买 nike

我正在使用 Retrofit 来集成我的 Web 服务,但我不明白如何使用 POST 请求将 JSON 对象发送到服务器。我目前卡住了,这是我的代码:

Activity :-

@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();

}

PostInterface:-

public interface PostInterface {

@POST("User/DoctorLogin")
Call<String> getStringScalar(@Body String body);
}

请求 JSON:-

{
"email":"device3@gmail.com",
"password":"1234"
}

响应 JSON:-

{
"error": false,
"message": "User Login Successfully",
"doctorid": 42,
"active": true
}

最佳答案

在gradle中使用这些

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'

Use these two POJO class ........

LoginData.class

public class LoginData {

private String email;
private String password;

public LoginData(String email, String password) {
this.email = email;
this.password = password;
}

/**
*
* @return
* The email
*/
public String getEmail() {
return email;
}

/**
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}

/**
*
* @return
* The password
*/
public String getPassword() {
return password;
}

/**
*
* @param password
* The password
*/
public void setPassword(String password) {
this.password = password;
}

}

LoginResult.class

public class LoginResult {

private Boolean error;
private String message;
private Integer doctorid;
private Boolean active;

/**
*
* @return
* The error
*/
public Boolean getError() {
return error;
}

/**
*
* @param error
* The error
*/
public void setError(Boolean error) {
this.error = error;
}

/**
*
* @return
* The message
*/
public String getMessage() {
return message;
}

/**
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}

/**
*
* @return
* The doctorid
*/
public Integer getDoctorid() {
return doctorid;
}

/**
*
* @param doctorid
* The doctorid
*/
public void setDoctorid(Integer doctorid) {
this.doctorid = doctorid;
}

/**
*
* @return
* The active
*/
public Boolean getActive() {
return active;
}

/**
*
* @param active
* The active
*/
public void setActive(Boolean active) {
this.active = active;
}

}

Use API like this

public interface RetrofitInterface {
@POST("User/DoctorLogin")
Call<LoginResult> getStringScalar(@Body LoginData body);
}

use call like this ....

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("Your domain URL here")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();

RetrofitInterface service = retrofit.create(RetrofitInterface .class);

Call<LoginResult> call=service.getStringScalar(new LoginData(email,password));
call.enqueue(new Callback<LoginResult>() {
@Override
public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
//response.body() have your LoginResult fields and methods (example you have to access error then try like this response.body().getError() )

}

@Override
public void onFailure(Call<LoginResult> call, Throwable t) {
//for getting error in network put here Toast, so get the error on network
}
});

编辑:-

put this inside the success() ....

if(response.body().getError()){
Toast.makeText(getBaseContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();


}else {
//response.body() have your LoginResult fields and methods (example you have to access error then try like this response.body().getError() )
String msg = response.body().getMessage();
int docId = response.body().getDoctorid();
boolean error = response.body().getError();

boolean activie = response.body().getActive()();
}

注意:- 始终使用 POJO 类,它在改造中删除了 JSON 数据解析。

关于android - 使用 Retrofit2 在 POST 请求中发送 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40817362/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com