我有一个请求参数的url在JsonFormat中 {" EmailAddress":" user@gmail.com"," PassWord":"密码"}它的请求参数。 当我在POSTMAN中使用它然后它的okey.but当我请求程序然后那个时候我得到了错误响应。我已经尝试过,请看这个片段。
0 Name at Address 11111 Pl
1 Name at Address 36 Crt
2 Name at Address 5678 cl
3 Name at Address 7 St
之后我使用了Interface。
public class LoginModel {
@SerializedName("EmailAddress")
public String userName;
@SerializedName("PassWord")
public String userPass;
}
@Override
public String toString() {
Log.e("POSTLOGIN_MODEL" , userName+"||"+userPass);
return "{" +
"EmailAddress='" + userName + '\'' +
", PassWord='" + userPass + '\'' +
'}';
}
}
之后,我曾经通过活动调用此接口方法。
public interface ApiService {
@FormUrlEncoded
@POST("/json/syncreply/AuthenticateUserRequest?")
Call<LoginResponse> LoginService(@Field("EmailAddress") String userName, @Field("PassWord") String userPass, Callback<LoginResponse> callBack);
在预先感谢。任何答案都会适用。我的英语是请避免它。
答案 0 :(得分:2)
嘿Rajan使用Request body传递Json
String request = "{\"EmailAddress\":\"raj@gmail.com\"," + "\"PassWord\":\"pass\"}";
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),request);
@Headers("Content-Type: application/json; charset=utf-8")
@POST("/json/syncreply/AuthenticateUserRequest")
Call<ResponseBody> AuthenticateUserRequest(@Body RequestBody body);
aCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
ResponseBody responseBody = response.body();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
答案 1 :(得分:0)
首先在你的休息客户端界面上更改下面的方法,而不是单独使用电子邮件和密码,在那里单独使用一个StringList:
@FormUrlEncoded
@POST(WEBSERVICE_NAME)
Call<ModelClass> methodName(
@Field("parameters" + "[]") ArrayList<String> paramsArrayList
);
现在,使用像这样的GSON库将模型类的arraylist转换为JSON字符串,
private ArrayList<String> getModelClassArrayinString(ArrayList<ModelClass> arrayList) {
ArrayList<String> arrayListString = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
arrayListString.add(new Gson().toJson(arrayList.get(i)).toString());
}
return arrayListString;
}
所以你最后的电话会是这样的:
Call<LoginResponse> call = mAPIService.LoginService(getModelClassArrayinString(arrayListofModelClass), new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.e("TAG" , "RESPONSE"+"||"+response.body());
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.e("TAG" , "FAILURE"+"||"+t.getMessage());
}
});