gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:14 24 4
gpt4 key购买 nike

I have one url with request parameter is in JsonFormat like {"EmailAddress":"user@gmail.com","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 = "test@gmail.com";
loginCredentials.userPass = "password";
String request = "{\"EmailAddress\":\"raj@gmail.com\"," +
"\"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.

最佳答案

嘿 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) {

}
});

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

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