我使用Volley库将4个文本和1个图像发布到mysql数据库。代码只工作一次之后,当我尝试发布时,我只获得空响应,并且在sql db中也没有看到数据。在Android清单中添加了所有必要的权限,但无法理解此代码的错误。
JAVA:
public class Main extends AppCompatActivity implements View.OnClickListener {
RadioButton radioType;
RadioGroup radioGroup;
EditText FoodName,FoodLocation,ShopName;
TextView Camera,Gallery,Post;
ImageView imageView;
private Bitmap bitmap;
private int PICK_IMAGE_REQUEST = 1;
private String UPLOAD_URL ="http://xyz.ttt.com/upload.php";
private String KEY_IMAGE = "image";
private String KEY_NAME = "FoodName";
private String KEY_LOCATION = "FoodLocation";
private String KEY_SHOP = "ShopName";
private String KEY_TYPE = "Type";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup)findViewById(R.id.RadioGroup);
FoodName = (EditText)findViewById(R.id.FoodName);
FoodLocation = (EditText)findViewById(R.id.FoodLocation);
ShopName = (EditText)findViewById(R.id.ShopName);
Camera = (TextView)findViewById(R.id.Camera);
Gallery = (TextView)findViewById(R.id.Gallery);
Post = (TextView)findViewById(R.id.Post);
imageView=(ImageView)findViewById(R.id.image);
Camera.setOnClickListener(this);
Gallery.setOnClickListener(this);
Post.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if(v == Gallery){
showFileChooser();
}
if(v == Post){
uploadImage();
}
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(){
//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(Main.this, s, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(Main.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String FName = FoodName.getText().toString().trim();
String FLocation = FoodLocation.getText().toString().trim();
String SName = ShopName.getText().toString().trim();
int selectedId = radioGroup.getCheckedRadioButtonId();
radioType = (RadioButton)findViewById(selectedId);
String rType = radioType.getText().toString().trim();
//Creating parameters
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, FName);
params.put(KEY_LOCATION, FLocation);
params.put(KEY_SHOP, SName);
params.put(KEY_TYPE, rType);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
} }
PHP:
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
$FoodName = $_POST['FoodName'];
$FoodLocation = $_POST['FoodLocation'];
$ShopName = $_POST['ShopName'];
$Type = $_POST['Type'];
require_once('dbConnect.php');
$sql ="SELECT id FROM foodstore ORDER BY id ASC";
$res = mysqli_query($con,$sql);
$id = 0;
while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}
$path = "images/$id.png";
$actualpath = "http://xyz.ttt.com/$path";
$sql = "INSERT INTO foodstore (image,FoodName,FoodLocation,ShopName,Type) VALUES ('$actualpath','$FoodName','$FoodLocation','$ShopName','$Type')";
if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}
mysqli_close($con);
}else{
echo "Error";
}
上述代码有什么问题?