2

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
  • 126
  • 1
  • 1
  • 12
  • Check this http://stackoverflow.com/a/21423093/3442067 – uday Mar 24 '17 at 05:33
  • Hello @uday please can you see the my programming steps what i did and if you find any thing wrong please let me inform thanks. – RAJAN Mar 24 '17 at 05:40

2 Answers2

2

Hey Rajan use Request body to pass 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) {

                }
            });
Piyush Patel
  • 371
  • 1
  • 5
  • 13
  • @Rajan your API will expecting JSON so you need to Pass in data in body for more refer this link https://futurestud.io/tutorials/retrofit-send-objects-in-request-body – Piyush Patel Mar 24 '17 at 06:14
  • thanks @Piyush Patel now cursor go to on Response(). – RAJAN Mar 24 '17 at 06:29
  • Then do Accept answer so other can Identify which one is right... Thanks – Piyush Patel Mar 24 '17 at 06:36
  • Hello @Piyush Patel i got onResponse(){} ->Response{protocol=http/1.1, code=200, message=OK, url=http://cii-api.fdfds.com?} so i want response text so i will get – RAJAN Mar 24 '17 at 06:41
  • hello @Piyush Patel i am not getting response data only i got response code is 200 but content how to get? – RAJAN Mar 24 '17 at 12:59
  • Hello @RAJAN Please check I have updated code for get response – Piyush Patel Mar 27 '17 at 05:30
0

First on your rest client interface side change the method like below, instead of taking email and password both seperately take only one ArrayList of String there :

@FormUrlEncoded
    @POST(WEBSERVICE_NAME)
    Call<ModelClass> methodName(
            @Field("parameters" + "[]") ArrayList<String> paramsArrayList            
        );

Now, convert your arraylist of model class in to JSON string using GSON library like this,

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;
    }

So your final call will be like this :

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

            }
        });
Ronak Joshi
  • 1,553
  • 2
  • 20
  • 43