无法 post 使用 Retrofit 作为原始数据 JSON
Unable to post data using Retrofit as raw JSON
我正在尝试 post 使用原始 json 中的 retrofit2 将数据发送到服务器,但是没有来自 server.Progress 的响应,栏一直在加载,但是没有来自服务器的响应。
我的服务器正在发送以下 json 对象作为成功消息:
{
"status": "Success",
"statusCode": "S001",
"loginResponse": {
"authenticationStatus": false,
"sessionid": null,
"errorDescription": null,
"predictionStatus": null,
"predictionPercentage": null,
"predictionPercentageA": null,
"predictionPercentageB": null,
"predictionPercentageC": null
}
}
我只想检查 status
是否是 Success
所以我在 POJO class LoginResponse
下面做了
登录响应
public class LoginResponse {
@SerializedName("status")
String status;
public LoginResponse(){
}
public LoginResponse(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
下面是我的代码:
ApiService.class
public interface ApiService {
@POST("userLoginVerification")
Call<LoginResponse> getResponse(@Body JsonObject body);
}
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = findViewById(R.id.login);
email = findViewById(R.id.email);
pwd = findViewById(R.id.pwd);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog prg = new ProgressDialog(MainActivity.this);
prg.setCancelable(false);
prg.setMessage("Logging in...");
prg.show();
String str1 = email.getText().toString();
String str2 = pwd.getText().toString();
if(str1.equals("")){
prg.dismiss();
TastyToast.makeText(getApplicationContext(),"Enter email",TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}
else if(str2.equals("")){
prg.dismiss();
TastyToast.makeText(getApplicationContext(),"Enter password",TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}
else{
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
JsonObject jsonObject= new JsonObject();
jsonObject.addProperty("userId",str1);
jsonObject.addProperty("password",str2);
Call<LoginResponse> call = apiService.getResponse(jsonObject);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
try{
JSONObject jsonObject1 = new JSONObject(resp);
String str = jsonObject1.getString("status");
if(str.equals("Success")){
prg.dismiss();
email.setText("");
pwd.setText("");
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
TastyToast.makeText(getApplicationContext(),t.getMessage(),TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}
});
}
}
});
}
执行上述操作时服务器没有响应code.Someone请告诉我我在做什么wrong.Any将不胜感激。
谢谢
在您的 ApiService 中
@FormUrlEncoded
@POST("userLoginVerification")
Call<User> getResponse(@FieldMap HashMap<String, String> data);
和 HashMap
HashMap<String, String> map = new HashMap<>();
map.put("userId", "theUserId");
map.put("password", "thePassword");
此外,一个大问题是您的回复格式
{ "status": "Success" }
但是在 ApiService
中你期望服务器 return 一个用户!
您必须更改 getResponse
return 类型以匹配服务器响应格式。
另一个问题在行
if (response.equals("Success"))
这个条件永远不会成立,因为 response
不是 String
并且你不能用这种方式检查相等性。你应该使用 response.body()
.
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response)
不要在 onResponse
中创建 JSONObject
。 response.body()
给你 LoginResponse 对象。使用该对象获取状态。 onResponse
或 onFailure
如果 activity 或片段未被销毁,请立即关闭进度条。
我正在尝试 post 使用原始 json 中的 retrofit2 将数据发送到服务器,但是没有来自 server.Progress 的响应,栏一直在加载,但是没有来自服务器的响应。
我的服务器正在发送以下 json 对象作为成功消息:
{
"status": "Success",
"statusCode": "S001",
"loginResponse": {
"authenticationStatus": false,
"sessionid": null,
"errorDescription": null,
"predictionStatus": null,
"predictionPercentage": null,
"predictionPercentageA": null,
"predictionPercentageB": null,
"predictionPercentageC": null
}
}
我只想检查 status
是否是 Success
所以我在 POJO class LoginResponse
下面做了
登录响应
public class LoginResponse {
@SerializedName("status")
String status;
public LoginResponse(){
}
public LoginResponse(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
下面是我的代码:
ApiService.class
public interface ApiService {
@POST("userLoginVerification")
Call<LoginResponse> getResponse(@Body JsonObject body);
}
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = findViewById(R.id.login);
email = findViewById(R.id.email);
pwd = findViewById(R.id.pwd);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog prg = new ProgressDialog(MainActivity.this);
prg.setCancelable(false);
prg.setMessage("Logging in...");
prg.show();
String str1 = email.getText().toString();
String str2 = pwd.getText().toString();
if(str1.equals("")){
prg.dismiss();
TastyToast.makeText(getApplicationContext(),"Enter email",TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}
else if(str2.equals("")){
prg.dismiss();
TastyToast.makeText(getApplicationContext(),"Enter password",TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}
else{
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
JsonObject jsonObject= new JsonObject();
jsonObject.addProperty("userId",str1);
jsonObject.addProperty("password",str2);
Call<LoginResponse> call = apiService.getResponse(jsonObject);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
try{
JSONObject jsonObject1 = new JSONObject(resp);
String str = jsonObject1.getString("status");
if(str.equals("Success")){
prg.dismiss();
email.setText("");
pwd.setText("");
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
TastyToast.makeText(getApplicationContext(),t.getMessage(),TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}
});
}
}
});
}
执行上述操作时服务器没有响应code.Someone请告诉我我在做什么wrong.Any将不胜感激。
谢谢
在您的 ApiService 中
@FormUrlEncoded
@POST("userLoginVerification")
Call<User> getResponse(@FieldMap HashMap<String, String> data);
和 HashMap
HashMap<String, String> map = new HashMap<>();
map.put("userId", "theUserId");
map.put("password", "thePassword");
此外,一个大问题是您的回复格式
{ "status": "Success" }
但是在 ApiService
中你期望服务器 return 一个用户!
您必须更改 getResponse
return 类型以匹配服务器响应格式。
另一个问题在行
if (response.equals("Success"))
这个条件永远不会成立,因为 response
不是 String
并且你不能用这种方式检查相等性。你应该使用 response.body()
.
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response)
不要在 onResponse
中创建 JSONObject
。 response.body()
给你 LoginResponse 对象。使用该对象获取状态。 onResponse
或 onFailure
如果 activity 或片段未被销毁,请立即关闭进度条。