doudi2005 2016-03-19 10:33
浏览 48

Volley post只对图像和文本一起工作到mysql db

I am posting 4 text and 1 image to mysql db using the Volley library. The code works only once after that i am getting only empty response when i try to post and no data is seen in the sql db as well. Added all the necessary permissions in the Android manifest as well but not able to fond what is wrong with this code.

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";
}

What is wrong with the above code?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?