0

I'm writing an API in Flask that takes user details and inserts it into the database. And I'm using Retrofit on Android to work with this API. Everything works fine but somehow the user input values are not being passed to the API. database gets updated with null values and not the values that the user has provided.

The database does get updated if I add parameter values manually in the URL using Postman i.e.(hostname/add_user?username="abc"&password="abc"....)

I'm new to retrofit, so I don't know where I'm going wrong exactly!

Flask API:

@app.route('/add_user', methods=['POST'])
def add_user():

username    = request.args.get('username')
password    = request.args.get('password')
name        = request.args.get('name')
age         = request.args.get('age')
gender      = request.args.get('gender')
cnumber     = request.args.get('cnumber')
address     = request.args.get('address')
city_id     = request.args.get('city_id')
email       = request.args.get('email')

t   = (username,password,name,age,gender,cnumber,address,city_id,email) #Storing all the received values in tuple

cur = mysql.connection.cursor()

cur.execute("INSERT INTO users (username,password,name,age,gender,contactnumber,address,city_id,email) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)", t)
    mysql.connection.commit()

    resp = jsonify("User added successfully")
    resp.status_code = 200
    resp.headers['Success'] = 'Success'

    return resp

JAVA CODE:

Call<ResponseBody> call = API_Client.getInstance().getAPI().add_user(username.getText().toString(), password.getText().toString(), name.getText().toString(), Integer.parseInt(age.getText().toString()), gender.getSelectedItem().toString(), cnumber.getText().toString(), address.getText().toString(), city.getId(), email.getText().toString());
                    call.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            try {
                                String s = response.body().string();
                                Toast.makeText(UserSignup.this, s, Toast.LENGTH_SHORT).show();

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {

                            Toast.makeText(UserSignup.this, t.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        }
                    });

API Interface:

@FormUrlEncoded
@POST("add_user")
Call<ResponseBody> add_user(

        @Field("username") String username,
        @Field("password") String password,
        @Field("name") String name,
        @Field("age") int age,
        @Field("gender") String gender,
        @Field("cnumber") String cnumber,
        @Field("address") String address,
        @Field("city_id") int city_id,
        @Field("email") String email
);

}

Zaid Waseem
  • 341
  • 1
  • 4
  • 13

1 Answers1

0

I think this must be a GET request hostname/add_user?username="abc"&password="abc". Change your API Interface to this or just replace @Field with @Query

@GET("add_user")
Call<ResponseBody> add_user(

        @Query("username") String username,
        @Query("password") String password,
        @Query("name") String name,
        @Query("age") int age,
        @Query("gender") String gender,
        @Query("cnumber") String cnumber,
        @Query("address") String address,
        @Query("city_id") int city_id,
        @Query("email") String email
);
Bek
  • 7,790
  • 4
  • 18
  • 31
  • Can you tell me how can this be done by using POST and not GET? – Zaid Waseem Sep 09 '18 at 10:08
  • If you want to make post request without changing server side code, then just replace `@Field` with `@Query`. Leave `@FormUrlEncoded @POST("add_user")` . I think this works with retrofit. – Bek Sep 09 '18 at 16:20