当我将图片和其他数据一起上载到数据库时,总是会失败

时间:2019-02-13 12:35:06

标签: php android retrofit2

我想通过Android应用程序将图像上传到服务器时遇到问题,但是我还希望将其他数据与图像一起传递

我为该过程创建了一个php文件

if (isset($_FILES['image']['name'])) {

    $nim = $_POST['nim'];
    $username = $_POST['username'];
    $nama = $_POST['nama'];
    $jk = $_POST['jk'];
    $fakultas = $_POST['fakultas'];
    $prodi = $_POST['prodi'];
    $angkatan = $_POST['angkatan'];
    $provinsi = $_POST['provinsi'];
    $nm_kabupaten = $_POST['nm_kabupaten'];
    $nm_kecamatan = $_POST['nm_kecamatan'];
    $nm_kelurahan = $_POST['nm_kelurahan'];
    $nm_lat = $_POST['nm_lat'];
    $nm_lng = $_POST['nm_lng'];

    $image = $_FILES['image']['name'];
    $tmp = $_FILES['image']['tmp_name'];
    $ukuran_gambar = $_FILES['image']['size'];
    $ukuran = 1000000;

    $gambarbaru = date('dmYHis').$image;
    if (!empty($image)) {
        $path = "foto/".$gambarbaru;
        if ($ukuran_gambar >= $ukuran) {
            $response["value"]=0;
            $response["message"]="Image Size Must be 1 MB";
            echo json_encode($response);
        }else{
            $sql1 = "SELECT * FROM mahasiswa WHERE nim = '$nim'";
            $check = mysqli_fetch_array(mysqli_query($con,$sql1));
            if (isset($check)) {
                $response["value"] = 2;
                $response["message"] = "Nim Has Already Taken";
                echo json_encode($response);
            }else{
                if (move_uploaded_file($tmp, $path)) {
                    $sql = "INSERT INTO mahasiswa (nim, username, nama, jk, image, fakultas, prodi, angkatan, provinsi, nm_kabupaten, nm_kecamatan, nm_kelurahan, nm_lat, nm_lng) VALUES ('$nim', '$username', '$nama', '$jk', '$gambarbaru', '$fakultas', '$prodi', '$angkatan', '$provinsi', '$nm_kabupaten', 
                    '$nm_kecamatan', '$nm_kelurahan', '$nm_lat', '$nm_lng');";
                    $hasil = mysqli_query($con, $sql);
                    if (!$hasil) {
                        die ('SQL Error: ' . mysqli_error($con));
                    }
                    if ($hasil) {
                        $response["value"] = 1;
                        $response["message"] = "Sucess";
                        echo json_encode($response);
                    }else{
                        $response["value"] = 3;
                        $response["message"] = "Failed";
                        echo json_encode($response);
                    }
                }
            }
        }
    }
}else{
    $response["value"] = 4;
    $response["message"] = "Not Image";
    echo json_encode($response);
}

这是我在Android中的上传功能:

int selectedId = radioJk.getCheckedRadioButtonId();
    RadioButton radioSexButton = findViewById(selectedId);

    File imageFile = new File(partImage);
    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),imageFile);

    String nim = edtNim.getText().toString().trim();
    String username = edtUsername.getText().toString();
    String nama = edtNama.getText().toString().trim();
    String jk = radioSexButton.getText().toString().trim();
    MultipartBody.Part gambarbaru = MultipartBody.Part.createFormData("imageupload", imageFile.getName(),requestBody);
    String fakultas = edtFakultas.getText().toString().trim();
    String prodi = edtProdi.getText().toString().trim();
    String angkatan = spinAngkatan.getSelectedItem().toString().trim();
    String provinsi = edtProvinsi.getText().toString().trim();
    String nm_kabupaten = spinKabupatenM.getSelectedItem().toString().trim();
    String nm_kecamatan = spinKecamatanM.getSelectedItem().toString().trim();
    String nm_kelurahan = spinKelurahanM.getSelectedItem().toString().trim();
    String nm_lat = spinLat.getSelectedItem().toString().trim();
    String nm_lng = spinLng.getSelectedItem().toString().trim();

    RequestBody nimReq = RequestBody.create(MediaType.parse("text/plain"), nim);
    RequestBody usernameReq = RequestBody.create(MediaType.parse("text/plain"), username);
    RequestBody namaReq = RequestBody.create(MediaType.parse("text/plain"), nama);
    RequestBody jkReq = RequestBody.create(MediaType.parse("text/plain"), jk);
    RequestBody fakultasReq = RequestBody.create(MediaType.parse("text/plain"), fakultas);
    RequestBody prodiReq = RequestBody.create(MediaType.parse("text/plain"), prodi);
    RequestBody angkatanReq = RequestBody.create(MediaType.parse("text/plain"), angkatan);
    RequestBody provinsiReq = RequestBody.create(MediaType.parse("text/plain"), provinsi);
    RequestBody kabupatenReq = RequestBody.create(MediaType.parse("text/plain"), nm_kabupaten);
    RequestBody kecamatanReq = RequestBody.create(MediaType.parse("text/plain"), nm_kecamatan);
    RequestBody kelurahanReq = RequestBody.create(MediaType.parse("text/plain"), nm_kelurahan);
    RequestBody latitudeReq = RequestBody.create(MediaType.parse("text/plain"), nm_lat);
    RequestBody longtitudeReq = RequestBody.create(MediaType.parse("text/plain"), nm_lng);

    if (nim.isEmpty()){
        progressDialog.dismiss();
        edtNim.setError("NIM harus Diisi");
        edtNim.requestFocus();
        return;
    }
    if (nama.isEmpty()){
        progressDialog.dismiss();
        edtNama.setError("Nama Harus Diisi");
        edtNama.requestFocus();
        return;
    }

    Call<Value> call = RetrofitClient.getmInstance().getApi().insertMhs(nimReq, usernameReq, namaReq, jkReq, gambarbaru, fakultasReq, prodiReq, angkatanReq, provinsiReq, kabupatenReq, kecamatanReq, kelurahanReq, latitudeReq, longtitudeReq);
    call.enqueue(new Callback<Value>() {
        @Override
        public void onResponse(Call<Value> call, Response<Value> response) {
            progressDialog.dismiss();
            String value = response.body().getValue();
            String message = response.body().getMessage();
            switch (value) {
                case "1":
                    Toast.makeText(AddMahasiswaActivity.this, message, Toast.LENGTH_LONG).show();
                    finish();
                    break;
                case "0":
                    Toast.makeText(AddMahasiswaActivity.this, message, Toast.LENGTH_SHORT).show();
                    break;
                case "2":
                    Toast.makeText(AddMahasiswaActivity.this, message, Toast.LENGTH_SHORT).show();
                    break;
                case "3":
                    Toast.makeText(AddMahasiswaActivity.this, message, Toast.LENGTH_SHORT).show();
                    break;
                case "4":
                    Toast.makeText(AddMahasiswaActivity.this, message, Toast.LENGTH_SHORT).show();
                    break;
            }
        }

        @Override
        public void onFailure(Call<Value> call, Throwable t) {
            progressDialog.dismiss();
            t.printStackTrace();
            Toast.makeText(AddMahasiswaActivity.this, "Gagal Merespon", Toast.LENGTH_SHORT).show();
        }
    });

我的ApiRequest:

@Multipart
@POST("mahasiswa/create.php")
Call<Value> insertMhs(@Part("nim") RequestBody nim,
                      @Part("username") RequestBody username,
                      @Part("nama") RequestBody nama,
                      @Part("jk") RequestBody jk,
                      @Part MultipartBody.Part image,
                      @Part("fakultas") RequestBody fakultas,
                      @Part("prodi") RequestBody prodi,
                      @Part("angkatan") RequestBody angkatan,
                      @Part("provinsi") RequestBody provinsi,
                      @Part("nm_kabupaten") RequestBody nm_kabupaten,
                      @Part("nm_kecamatan") RequestBody nm_kecamatan,
                      @Part("nm_kelurahan") RequestBody nm_kelurahan,
                      @Part("nm_lat") RequestBody nm_lat,
                      @Part("nm_lng") RequestBody nm_lng);

当我在Postman中进行测试时,结果正常(图像和其他数据成功上传到数据库中)。但是,当我在android应用中进行测试时,结果始终无法上传到数据库中,并且显示的消息为“ Not Image”。

0 个答案:

没有答案