java - 如何在Android POST方法中与body一起使用Retrofit?

标签 java android retrofit retrofit2

I have one url with request parameter is in JsonFormat like {"EmailAddress":"[email protected]","PassWord":"password"} it's requested parameter. When i used in POSTMAN then its okey.but when i request with program then that time i got error response. ihave tried till like this please see this snippet.

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 + '\'' +
            '}';

}
}

After that i used Interface.

public interface ApiService {

@FormUrlEncoded
@POST("/json/syncreply/AuthenticateUserRequest?")
Call<LoginResponse> LoginService(@Field("EmailAddress") String userName,          @Field("PassWord") String userPass, Callback<LoginResponse> callBack);

After that i used to call this interface method through activity.

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(input_username.getText().toString() != null && input_password.getText().toString() != null
                     && !input_username.getText().toString().isEmpty() && !input_password.getText().toString().isEmpty()){
                LoginModel loginCredentials = new LoginModel();
                loginCredentials.userName = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="017564727541666c60686d2f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>";
                loginCredentials.userPass = "password";
                String request = "{\"EmailAddress\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2809398b2959f939b9edc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>\"," +
                        "\"PassWord\":\"pass\"}";
                sendPost(loginCredentials);

            }else{
                Toast.makeText(getApplicationContext() , "Please enter valid Username and Password." , Toast.LENGTH_LONG).show();
            }
        }
    });

public void sendPost(LoginModel name) {
    Log.e("TAG","||"+name.userPass+"||"+name.userName);
   // mAPIService.savePost(name).enqueue(new Callback<LoginModel>() {
        Call<LoginResponse> call = mAPIService.LoginService(name.userName, name.userPass, 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());

            }
        });

     }

Thanks In Advance.any answer will appriciated.my english is please avoid it.

最佳答案

Hey Rajan 使用请求正文传递 Json

String request = "{\"EmailAddress\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6517040f250208040c094b060a08" rel="noreferrer noopener nofollow">[email protected]</a>\"," + "\"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) {

                }
            });

关于java - 如何在Android POST方法中与body一起使用Retrofit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42992029/

相关文章:

android - 使用 mupdf 库的 Pdf 查看器

android - 使用 retrofit 2 下载 POST 方法的 PDF 响应

Android:使用 Moshi 适配器解析内部非结构化 Json

java - 检查 j2me 中的互联网是否正常工作

java - Android开发,Gottox socket.io-java-client : file not fount Exception/socket. io/1/

java - Eclipse Debug模式更改变量

android - 使用包含参数的 json 对象改造 POST

android - 无法在 Retrofit 2.0 android 中获取 json 字符串作为响应

java - 为企业 Java 与 C++ 创建 Web 服务应用程序?

java - 联系电话验证