Home » Php » java – Upload Video via retrofit to php server getting error

java – Upload Video via retrofit to php server getting error

Posted by: admin February 25, 2020 Leave a comment


Im trying to upload video to php server but the files are not uploading to server getting onfailure response from server whats wrong in my code
im using retrofit to send data to server. I noticed that when ever i send data my data is consumed but never gets uploaded into my server.

java code

            public void sennwes() {

                // Parsing any Media type file

                RequestBody headline =RequestBody.create(MediaType.parse("text/plain") ,ed1.getText().toString());
                RequestBody news = RequestBody.create(MediaType.parse("text/plain") ,ed2.getText().toString());
                RequestBody date = RequestBody.create(MediaType.parse("text/plain") ,dob.getText().toString());
                RequestBody place = RequestBody.create(MediaType.parse("text/plain") ,place1.getText().toString());
                RequestBody cat = RequestBody.create(MediaType.parse("text/plain") ,cat1.getSelectedItem().toString());
                RequestBody file1 = RequestBody.create(MediaType.parse("text/plain") ,imagetostring());

                File file = new File(filePath.toString());

                MediaType MEDIA_TYPE = MediaType.parse("video/mp4");
                RequestBody requestBody = RequestBody.create(MEDIA_TYPE, file);
                MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
                RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());

                final ProgressDialog dialog = ProgressDialog.show(addnewnews.this, "",
                        "Loading. Please wait...", true);

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Config.ADMIN_PANEL_URL + "/")
                ApiInterface apiInterface = retrofit.create(ApiInterface.class);
                Log.e("Rs","response osthindi1");

                Call<Value> call = apiInterface.postNwes(headline, news, date, place,cat,file1,fileToUpload,filename);

                //Log.e("aks", String.valueOf(file));
                call.enqueue(new Callback<Value>() {
                    public void onResponse(Call<Value> call, final Response<Value> response) {
                        final String value = response.body().getValue();
                        final String message = response.body().getMessage();

                        new Handler().postDelayed(new Runnable() {
                            public void run() { Log.e("Rs","run osthindi");

                                if (value.equals("1")) {
                                    Toast.makeText(getApplicationContext(), R.string.msg_comment_success, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getApplicationContext(), R.string.msg_comment_failed, Toast.LENGTH_SHORT).show();
                        }, Constant.DELAY_REFRESH);


                    public void onFailure(Call<Value> call, Throwable t) {
                        Toast.makeText(getApplicationContext(), "Network Error!", Toast.LENGTH_SHORT).show();




Call<Value> postNwes(@Part("headline") RequestBody headline,
                     @Part("news") RequestBody user_id,
                     @Part("date") RequestBody content,
                     @Part("place") RequestBody date_time,
                     @Part("cat") RequestBody cat,
                     @Part("image") RequestBody img,
                     @Part MultipartBody.Part file,
                     @Part("name") RequestBody name


php server script

    public function postNwes() {

        if($_SERVER['REQUEST_METHOD'] == 'POST') {

               $response = array();
               //mendapatkan data
               $headline = $_POST['headline'];
               $news = $_POST['news'];
               $date = $_POST['date'];
               $place = $_POST['place'];
               $cat = $_POST['cat'];
               $imagepath = "../upload/$headline.jpg";
               $imageupload =$_POST['image'];

               $target_dir = "../upload/video/";

                $target_file_name = $target_dir .basename($_FILES['file']['name']); 
                $tempname =$_FILES['file']['tmp_name'];

               include "../includes/config.php";

               $sql = "INSERT INTO tbl_user_nwes2 (headline, news, date, place,cat, images,video) VALUES('$headline', '$news', '$date', '$place','$cat','$imagepath','$tempname')";

               if (mysqli_query($connect, $sql)) {

                 $response["value"] = 1;
                 $response["message"] = "success post ";
                 echo json_encode($response);
                 $imageupload = str_replace("\"", "",$imageupload  );
                 move_uploaded_file($tempname, $target_file_name);
               } else {
                 $response["value"] = 0;
                 $response["message"] = $imagepath;
                 echo json_encode($response);

             // tutup database

          else {
            $response["value"] = 0;
            $response["message"] = "oops! conn failed!";

            header( 'Content-Type: application/json; charset=utf-8' );
            echo json_encode($response);


I can’t able to guess whats wrong in this code thanks in advance

How to&Answers: