在android中使用volley和php发布utf8(波斯语或阿拉伯语)

ehxuflar  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(313)

我想通过php和volley向服务器发送波斯语文本;
问号被发送,或者在android上编码时,在mysql表中是这样的:
%d8%aa%d9%86%d9%86%d8%aa
 
我在android上使用了urlencoder.encode
在php中
@mysqli\u query($connect,“set character set utf8;”);
 
在mysql中,所有列都在两种模式下进行测试:utf8 general ci和posian ci
对不起,我的母语不是英语

String url = mylib.clsVars.get_url(2);
        progressDialog.show();
        progressDialog.setCancelable(false);

        final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        progressDialog.hide();
                        Log.i("response: ",response.toString() );
                        if (response.startsWith("")) {
                            response = response.replaceFirst(" ", "");
                        }

                            if (Objects.equals(response, "done"))
                            {
                                Toast.makeText(con, "با موفقیت ارسال شد", Toast.LENGTH_LONG).show();

                                edt_title.setText("");
                                edt_desc.setText("");
                                edt_group.setText("");
                                gid="";
                                bitmap1=null;
                                bitmap2=null;
                                bitmap3=null;

                                img1.setImageResource(R.drawable.no_image);
                                img2.setImageResource(R.drawable.no_image);
                                img3.setImageResource(R.drawable.no_image);

                            }else
                            {
                                Toast.makeText(con, response+"خطا در ارسال", Toast.LENGTH_LONG).show();

                            }

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

                progressDialog.hide();
                Log.i("VolleyError in insert: ",error.toString() );
                Toast.makeText(con, "خطا در ارسال", Toast.LENGTH_LONG).show();

            }
        }
        )
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map =new Hashtable<String,String>();
                String img1="",img2="",img3="";
                String title1=null,desc1=null,vid1=null,gid1=null;
                try {
                    title1= URLEncoder.encode(edt_title.getText().toString(), "UTF-8");
                     desc1= URLEncoder.encode(edt_desc.getText().toString(), "UTF-8");
                     vid1= URLEncoder.encode(edt_video.getText().toString(), "UTF-8");
                     gid1= URLEncoder.encode(gid, "UTF-8");

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                    map.put("t_title", title1);

                    map.put("t_desc", desc1);

                    map.put("t_vid", vid1);

                    map.put("gid", gid);

                map.put("t_map_lat", String.valueOf(map_lat));
                map.put("t_map_lon", String.valueOf(map_lon));

                if (bitmap1 != null)
                img1=image_base64(bitmap1);

                if (bitmap2 != null)
                    img2=image_base64(bitmap2);

                if (bitmap3 != null)
                    img3=image_base64(bitmap3);

                map.put("img1",img1);
                map.put("img2",img2);
                map.put("img3",img3);

                return map;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(con);

        requestQueue.add(stringRequest);

和php代码:

<?php

    require_once("****");

    $error = array();

    if( isset( $_POST['t_title'] )  && ( !empty( $_POST['t_title'] ) )  &&
        isset( $_POST['t_desc'] )  && ( !empty( $_POST['t_desc'] ) )  &&
        isset( $_POST['t_map_lat'] )  && ( !empty( $_POST['t_map_lat'] ) )  &&
        isset( $_POST['t_map_lon'] )   && ( !empty( $_POST['t_map_lon'] ) )   &&
        isset( $_POST['img1'] )    && ( !empty( $_POST['img1'] ) )
         &&
        isset( $_POST['img2'] )    && ( !empty( $_POST['img2'] ) )
         &&
        isset( $_POST['img3'] )    && ( !empty( $_POST['img3'] ) )
        &&

        isset( $_POST['gid'] )    && ( !empty( $_POST['gid'] ) )
         &&

        isset( $_POST['t_vid'] )    && ( !empty( $_POST['t_vid'] ) )      )

    {

        $t_title1        =$_POST['t_title'] ;
        $t_desc  = utf8_decode($_POST['t_desc']) ; 
        $t_map_lat       = $_POST['t_map_lat'] ;
        $t_map_lon    =   $_POST['t_map_lon'] ;  
        $cat       =  utf8_decode(  $_POST['gid']) ;
        $t_vid      =   utf8_decode(  $_POST['t_vid']) ;
        $t_title = mb_convert_encoding($t_title1,'HTML-ENTITIES','utf-8');

        $rand =   rand( rand(5 , 50) , rand( 500 , 900 ) );

        $location1 = "pics/"   .$rand.
                    "_" . date("i") . "_" . date("d-m-Y") ."1". ".jpg";

        $location2 = "pics/"   .$rand.
                                       "_" . date("i") . "_" . date("d-m-Y")."2" . ".jpg";

        $location3 = "pics/"   .$rand.
                                        "_" . date("i") . "_" . date("d-m-Y") ."3". ".jpg";

        $img1      =base64_decode(  $_POST['img1']);
        $img2      =base64_decode(  $_POST['img1']);
        $img3       =base64_decode(  $_POST['img3']);

        $resultOfCreatingImage1 = file_put_contents( $location1 , $img1 );
        $resultOfCreatingImage2 = file_put_contents( $location2 , $img2 );
        $resultOfCreatingImage3 = file_put_contents( $location3 , $img3 );

                    $img =  "http://tourist.tech9web.com/".$location1.";".
                    "http://tourist.tech9web.com/".$location2.";"."http://tourist.tech9web.com/".$location3;

        if( $resultOfCreatingImage1 == false )
        {
            $error['error'] = "failure_creating_image";
        }

            $query= "INSERT INTO places(t_title,t_desc,t_map_lat,t_map_lon,gid,t_img,t_vid) " .
                    "VALUES('".$t_title."', '".$t_desc."', '".$t_map_lat."', '".$t_map_lon."', '".$cat."','".$img."','".$t_vid."')";

            $connect = @mysqli_connect( $hostname , $username , $password , $database );
            if( $connect )
            {
                @mysqli_query( $connect , "SET CHARACTER SET utf8;" );

                @mysqli_query( $connect , $query );

                if( @mysqli_affected_rows( $connect ) > 0 )
                {
                    $error['error'] = "done";
                }
                else
                {
                    @unlink( $location );

                    $error['error'] = "failure_inserting_database!";
                }
            }
            else
            {
                @unlink( $location );

                $error['error'] = "failure_connecting_database";
            }
            }
    else
    {
         $error['error'] = "failure_post";
    }

    die ( json_encode( $error['error'] ) );

 ?>
tyu7yeag

tyu7yeag1#

尝试将表排序规则更改为 utf8_unicode_ci

0mkxixxg

0mkxixxg2#

排序规则与编码无关。
我猜你在期待 'تننت' ? 当编码到 CHARACTER SET utf8 (或 utf8mb4 ),即十六进制 D8AAD986D986D8AA (没有百分号)。
你真的有空格吗 % ?
在哪里 %xx 从哪里来?那真的在table上吗?或者有什么东西给你看。
为什么在bom表后面加一个空格 response = response.replaceFirst(" ", ""); ?
使用任何类型的编码器/解码器通常都会适得其反。
请提供 SELECT HEX(col) ... 所以我们可以看看table上是否只有 D8AAD986D986D8AA . 每个阿拉伯字母编码为2字节码: Dxyy

相关问题