Grace Jochu
Grace Jochu

Reputation: 1

Error in sending POST request using retrofit

I have a json data to be sent using retrofit in android howerver it's not not getting sent to the server. I have used slim framework at the server side .

this is my interface in android client side

    public interface RequestInterface 
    {
    @Headers("Content-type: application/json")
    @POST("/instituteRegister")
    Call<InstRegServerResponse> sendInstRegData(@Body InstRegServerRequest 
    post);
    }

this is the sign up method

>       public void signup()
        {
        String regdName = _regdName.getText().toString();
        String email = _email.getText().toString();
        String password = _password.getText().toString();
        Log.d("password", password);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface requestInterface = 
        retrofit.create(RequestInterface.class);
        InstRegServerRequest instRegServerRequest = new InstRegServerRequest();
        instRegServerRequest.setiname(instituteName);
        instRegServerRequest.setemail(email);
        instRegServerRequest.setpassword(password);
        Call<InstRegServerResponse> response = 
        requestInterface.sendInstRegData(instRegServerRequest);
        response.enqueue(new Callback<InstRegServerResponse>() 
        {
            @Override
        public void onResponse(Call<InstRegServerResponse> call, 
        retrofit2.Response<InstRegServerResponse> response) 
        {
                InstRegServerResponse resp = response.body();
                Log.d("status:", "sign up success");
        }
            @Override
            public void onFailure(Call<InstRegServerResponse> call, Throwable t) 
            {
              Log.d(Constants.TAG,"signed up failed");
            }
        });
        }

The error is the JSON data is not passed to the server The api endpoint works correctly as i have tested it using postman in the android logcat i get sign up success but at the server side I think the json is not passed correctly that's why i'm unable o write the data to the database

Upvotes: 0

Views: 583

Answers (1)

khaleel_jageer
khaleel_jageer

Reputation: 1502

Try this.

@Headers("Content-Type: application/json; charset=UTF-8")
@POST("instituteRegister")
Call<InstRegServerResponse> sendInstRegData(@Body Map<String, Object> params);

Construct your JSON object using Map<String, Object>.

Example:

Map<String, Object> param = new HashMap<>();
param.put("YOUR_KEY", YOUR_VALUE);

Upvotes: 1

Related Questions