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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料