通过Android Studio将图像上传到000webhost.com

时间:2018-11-11 11:37:24

标签: php android mysql database android-volley

所以我试图将我的android studio连接到该Web服务器(000webhost),但似乎无法成功上传,我认为问题是路径,但是我没有在线找到一些有关如何连接到服务器的示例。 000webhost的文件管理器

connection.php     

header('Content-type : bitmap; charset=utf-8');

if(isset($_POST["encoded_image"])){

    $encoded_image = $_POST["encoded_image"];
    $image_name = $_POST["image_name"];

    $decoded_string = base64_decode($encoded_image);

    $path = 'images/'.$image_name;

    $file = fopen($path,'wb');

    $is_written = fwrite($file,$decoded_string);
    fclose($file);

    if($is_written > 0){
        $connection = mysqli_connect('localhost','id7794597_razzlesz','hahayz9090','id7794597_memes');
        $query = "INSERT INTO photos (name,path) values ('$image_name','$path');";

        $result = mysqli_query($connection, $query);

        if($result){
            echo "success";
            }else{
                echo "failed";
            }
            mysqli_close($connection);
    }
}

?>

btnUpload

btnUpload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, urlUpload, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }

        }){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                String imageData = imageToString(bitmap);
                String imgName = edtName.getText().toString();
                params.put ("encoded_image", imageData);
                params.put ("image_name", imgName);

                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(stringRequest);
        Toast.makeText(getApplicationContext(),"image uploaded!", Toast.LENGTH_LONG).show();
    }
});

my db

我应该在php的路径上放什么?

1 个答案:

答案 0 :(得分:0)

他的问题似乎是目录路径。 000webhost具有与我所了解的不同的目录路径。如果应用程序中没有错误消息,则它必须是在线数据库中的路径目录。