php - 使用带有 android 应用程序的 URL 将图像保存在服务器上 ERROR End of input error at line 1 column 1 path $
问题描述
我有一个android app
用户image
从中选择的位置gallery
,然后当他保存它时,它image
保存在UPLOADS
我的文件夹中,server
并在MySQL
表格url
中保存图像。
我retrofit
用来保存照片。
我有一个问题,当用户选择photo
并保存它时,应用程序返回错误End of input at line 1 column 1 path $
,但照片完美保存在服务器上,URL 保存在 MySQL 上。
但是End of input error at line 1 column 1 path $
我应该收到 Toast 消息,而不是收到。
我怎样才能停止收到该错误消息?
主要活动
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接口
@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);