php - 使用 Android 应用程序使用 URL 在服务器上保存图像 ERROR 输入错误结束在第 1 行第 1 列路径 $

标签 php android image retrofit

我有一个 android 应用程序,用户从 gallery 中选择一个 image,然后当他保存它时,image 保存在我的 server 上的 UPLOADS 文件夹中,在 MySQL 表中,图像 url 是已保存。

我使用 retrofit 来保存照片。

我有一个问题,当用户选择 photo 并保存它时,应用返回错误 End of input at line 1 column 1 path $但照片完美保存在服务器上,URL 保存在 MySQL 上

但我应该收到 Toast 消息,而不是在第 1 行第 1 列路径 $ 处收到 End of input error。

我怎样才能停止收到该错误消息?

MainActivity

public class MainActivity extends AppCompatActivity {

    Bitmap bitmap;
    ImageView imageView;
    Button selectImg,uploadImg;
    EditText imgTitle;
    private  static final int IMAGE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        selectImg = (Button) findViewById(R.id.button);
        uploadImg = (Button) findViewById(R.id.button2);
        imgTitle = (EditText) findViewById(R.id.editText);

        selectImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                selectImage();

            }
        });

        uploadImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                validateImage();

            }
        });

    }

    private void validateImage() {

        //find values
        final String regName = imgTitle.getText().toString();
        String image = convertToString();

        //        checking if username is empty
        if (TextUtils.isEmpty(regName)) {
            Toast.makeText(MainActivity.this, "Seleziona i metodi di pagamento del tuo locale.", Toast.LENGTH_LONG).show();
            return;
        }
        //checking if email is empty

        //checking if password is empty

        //After Validating we register User
        uploadImage(regName, image);

    }


    private void selectImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, IMAGE);
    }

    private String convertToString()
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
        byte[] imgByte = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(imgByte,Base64.DEFAULT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode== IMAGE && resultCode==RESULT_OK && data!=null)
        {
            Uri path = data.getData();

            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void uploadImage(String imageNome, String image){


        ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<Img_Pojo> call = apiInterface.uploadImage(imageNome,image);

       // ApiInterface apiInter = ApiClient.getApiClient().create(ApiInterface.class);
       // Call<Img_Pojo> call = apiInter.uploadImage(imageName,image);





        call.enqueue(new Callback<Img_Pojo>() {
            @Override
            public void onResponse(Call<Img_Pojo> call, Response<Img_Pojo> response) {

                if(Objects.requireNonNull(response.body()).getIsSuccess() == 1) {
                    Toast.makeText(MainActivity.this, "Informazioni inserite!", Toast.LENGTH_LONG).show();

                }

                 else{
                Toast.makeText(MainActivity.this,response.body().getMessage(),Toast.LENGTH_LONG).show();
            }


            }

            @Override
            public void onFailure(Call<Img_Pojo> call, Throwable t) {
                Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();

            }
        });

    }

}

Api 接口(interface)

@FormUrlEncoded
    @POST("up.php")
    Call<Img_Pojo> uploadImage(@Field("image_name") String title, @Field("image") String image);

ApiClient

private static final String BaseUrl = "https://provaord.altervista.org/OrdPlace/image2/";
    private static Retrofit retrofit;

    public static Retrofit getApiClient() {

        retrofit = new Retrofit.Builder().baseUrl(BaseUrl).
                addConverterFactory(GsonConverterFactory.create()).build();

        return retrofit;
    }

Img_Pojo

 private String Title;
        private String Image;

        private int isSuccess;
        private String message;

        public Img_Pojo(String Title, String Image,  int isSuccess, String message) {
            this.Title = Title;
            this.Image = Image;

            this.isSuccess = isSuccess;
            this.message = message;

        }

        public String getTitle() {
            return Title;
        }

        public void setTitle(String title) {
            this.Title = title;
        }

        public String getImage() {
            return Image;
        }

        public void setImage(String image) {
            this.Image = image;
        }



        public int getIsSuccess() {
            return isSuccess;
        }

        public void setIsSuccess(int success) {
            this.isSuccess = success;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }



    }

up.php

<?php

$image_name = $_POST['image_name'];
$image = $_POST['image'];

$path = "imagini/$image_name.jpg";

$output=array();

//require database
require_once('db.php');

$conn=$dbh->prepare('INSERT INTO volley_upload(image_name,image_path) VALUES (?,?)');
//encrypting the password

$conn->bindParam(1,$image_name);
$conn->bindParam(2,$path);
$conn->execute();

file_put_contents($path,base64_decode($image));

if($conn->rowCount() == 0)
{
$output['isSuccess'] = 0;
$output['message'] = "Registrazione fallita, riprovare.";
}
elseif($conn->rowCount() !==0){

$output['isSuccess'] = 1;
$output['message'] = "Informazioni base inserite!";

}

?>

最佳答案

当您的响应完全为空时会发生此错误。所以检查你必须检查 php 文件

正如我所见,您永远不会返回或打印输出以从 android 接收它。

在php文件末尾添加这一行

echo json_encode($output);

关于php - 使用 Android 应用程序使用 URL 在服务器上保存图像 ERROR 输入错误结束在第 1 行第 1 列路径 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56617774/

相关文章:

Android WebView资源文件如js和css文件缓存

安卓 : translate the language of whole app on click

javascript - 在 dom 准备好后,从 <img> 获取所有数据源并将它们设置在 src 属性中

php - 注销和登录用户的不同 WordPress 首页

php - 是否可以为所有用户只打开一个数据库连接?

php - 完成后使用 jQuery 向 iframe 反馈提交表单?

android - 如何在 ImageView 上获取图像的 ID?

python - 如何使用 python PIL 获取图像中的颜色集

html - 图像重复不起作用,为什么...?

javascript - Cordova 游戏 - 试图让我的网站发布在线高分