这是我的方法在服务器上发布两个字符串和一个图像这里我正确发送数据但是当我发送没有图像没有数据可以发送我想发送字符串和图像在所有情况下图像和没有图像。
private void Posts()
{
final StringRequest stringRequest=new StringRequest(Request.Method.POST, uploadeImageUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
String Response=jsonObject.getString("response");
Toast.makeText(Post.this,Response,Toast.LENGTH_LONG).show();
imagepost.setImageResource(0);
imagepost.setVisibility(View.GONE);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params =new HashMap<>();
params.put("name",editwriter.getText().toString().trim());
params.put("postcontent",editsubject.getText().toString().trim());
params.put("imagepost",imageToString(bitmap).trim());
return params;
}
};
Mysingletone.getInstance(Post.this).addToRequestque(stringRequest);
}
private void selectImages(){
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,IMG_REQUEST);
}
private String imageToString(Bitmap bitmap)
{
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgbystes = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgbystes,Base64.DEFAULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==IMG_REQUEST&&resultCode==RESULT_OK&&data!=null) {
Uri path = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
imagepost.setImageBitmap(bitmap);
imagepost.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
}
}
}}
$con= mysqli_connect($host_name,$user_name,$user_pass,$dp_name);
if($con)
{
date_default_timezone_set("Asia/Muscat");
$datee= date("Y/m/d");//strip_tags(trim($_POST["date"]));
$timee= date("h:i:sa");//strip_tags(trim($_POST["time"]));
$dt=date("Ymd");
$tm=date("his");
$name=$_POST['name'];
$postcontent=$_POST['postcontent'];
$image = $_POST["image"];
$imagepost= $_POST["imagepost"];
$rnd = rand(0, 5000);
$imgname="imagepost".$dt.$tm.$rnd.".jpg";
$url = "http://devsinai.com/DrSiani/imageUploadPostDr/images/".$imgname;
$sql="INSERT INTO posts (name,postcontent,imagepost) VALUE ('$name','$postcontent','$url')";
$sql2="INSERT INTO posts (name,postcontent,imagepost) VALUE ('$name','$postcontent')";
$uploads_path="images/$imgname";
if(mysqli_query($con,$sql))
{
file_put_contents($uploads_path,base64_decode($imagepost));
echo json_encode(array('response'=>'Successfully'));
}
else {
echo json_encode(array('response'=>'fiald'));
}
}
mysqli_close($con);
?>
答案 0 :(得分:0)
你能添加imageToString(Bitmap bitmap)的代码吗?
修改强> 首先将if语句添加到getParams()方法,如下所示:
String imageString = imageToString(bitmap);
if(!imageString.equals("false")){
params.put("imagepost",imageToString(bitmap).trim());
}
让imageToString方法返回&#34; false&#34;错误时为String。 像这样编辑你的php脚本:
$con= mysqli_connect($host_name,$user_name,$user_pass,$dp_name);
if($con){
date_default_timezone_set("Asia/Muscat");
$datee= date("Y/m/d");//strip_tags(trim($_POST["date"]));
$timee= date("h:i:sa");//strip_tags(trim($_POST["time"]));
$dt=date("Ymd");
$tm=date("his");
$name=$_POST['name'];
$postcontent=$_POST['postcontent'];
if(isset($_POST["image"])){
$imageChecker = TRUE;
$imagepost= $_POST["imagepost"];
$rnd = rand(0, 5000);
$imgname="imagepost".$dt.$tm.$rnd.".jpg";
$url = "http://devsinai.com/DrSiani/imageUploadPostDr/images/".$imgname;
$sql="INSERT INTO posts (name,postcontent,imagepost) VALUE ('$name','$postcontent','$url')";
} else {
$imageChecker = FALSE;
$sql="INSERT INTO posts (name,postcontent) VALUE ('$name','$postcontent')";
}
$uploads_path="images/$imgname";
if(mysqli_query($con,$sql)){
if($imageChecker)
file_put_contents($uploads_path,base64_decode($imagepost));
echo json_encode(array('response'=>'Successfully'));
} else {
echo json_encode(array('response'=>'fiald'));
}
}
mysqli_close($con);