0

I am trying to register user details like(nam, number, email, password and image). I have already written API in php and it's working fine if I post it from postman.

Here is postman screenshot :
enter image description here

But when I tried to send data from android then I am getting error message from server like "Required parameters (name, email or password) is missing!". It's working when i post from postman but not from my android code. I am surely doing something wrong.
Here is php api :

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Content-Type: application/json');
header('Access-Control-Allow-Headers: Authorization');
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("status" => 1);

if (isset($_POST['name']) && isset($_POST['number']) && isset($_POST['email']) && isset($_POST['password']) && isset( $_FILES[ 'image' ]) && isset($_POST['pns_token']) && isset($_POST['is_owner']) && isset($_POST['tbl_flat_id']) && isset($_POST['tbl_building_id']) && isset($_POST['tbl_society_id'])) {

    $name = $_POST['name'];
    $number = $_POST['number'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $pns_token = $_POST['pns_token'];
    $is_owner = $_POST['is_owner'];
    $tbl_flat_id = $_POST['tbl_flat_id'];
    $tbl_building_id = $_POST['tbl_building_id'];
    $tbl_society_id = $_POST['tbl_society_id'];


    //$guest_master = $db -> storeGuest($name, $number, $email, $password, $image);


    // check if user is already existed with the same email
    if ($db->isUserExisted($email)) {
        // user already existed
        $response["status"] = 1;
        $response["message"] = "User already existed with " . $email;
        echo json_encode($response);
    } else {
        // create a new user
        $user = $db->storeUser($name, $number, $email, $password, $image, $pns_token, $is_owner, $tbl_flat_id, $tbl_building_id, $tbl_society_id);
        if ($user) {
             return result
            $response["status"] = 0;
            $response["id"] = intval($user["userId"]);
            $response["message"] = "User registered successfully with " . $email;
            echo json_encode($response);
        } else {
            // user failed to store
            $response["status"] = 2;
            $response["message"] = "Unknown error occurred in registration!";
            echo json_encode($response);
        }
    }
} else {
    // receiving the post params
    $response["status"] = 3;
    $response["message"] = "Required parameters (name, email or password) is missing!";
    echo json_encode($response);
}
?>

Here is my android code :
NetworkService.java :

public interface NetworkService {
// retrofit get call
@FormUrlEncoded
@POST(Constants.USER_LOGIN)
Call<UserLoginResponse> executeLogin(@Field("email") String email,
                                     @Field("password") String password);

@FormUrlEncoded
@POST(Constants.USER_REGISTER)
Call<UserRegistrationResponse> executeRegistration(@Field("name") String name,
                                                   @Field("number") String number,
                                                   @Field("email") String email,
                                                   @Field("password") String password,
                                                   @Field("image") File imagePath,
                                                   @Field("pns_token") String pnsToken,
                                                   @Field("is_owner") int isOwner,
                                                   @Field("tbl_flat_id") int flatId,
                                                   @Field("tbl_building_id") int buildingId,
                                                   @Field("tbl_society_id") int societyId
                                                   );
}

Here is my RegisterActivity.java . :

NetworkService networkService = 
App.getClient(EXTENDED_URL).create(NetworkService.class);
Call<UserRegistrationResponse> call = 
networkService.executeRegistration(txtName.getText().toString(),
                            txtNumber.getText().toString(),
                            txtEmail.getText().toString(),
                            txtPassword.getText().toString(),
                            imagePath,
                            pnsToken,
                            0,
                            flatId,
                            buildingId,
                            societyId
                            );

Here is code which converting image to file in RegsiterActivity :

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imvPhoto.setImageBitmap(imageBitmap);

        Uri selectedImage = data.getData();

        imagePath = savebitmap("profile_photo", imageBitmap);
    }
}

private File savebitmap(String filename, Bitmap imageBitmap) {
    File filesDir = getFilesDir();
    File imageFile = new File(filesDir, filename + ".jpg");

    OutputStream os;
    try {
        os = new FileOutputStream(imageFile);
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
    }
    return imageFile;

}

Please provide my any reference or help so I can resolve this.

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160

1 Answers1

0

You have to use multipart on this.

There's a similar answer provided in the link below that tells you how to upload image with other data, you can check it out.

POST Multipart Form Data using Retrofit 2.0 including image

Jaype Sison
  • 61
  • 1
  • 1