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

时间:2018-04-14 13:40:32

标签: php android mysql utf-8 android-volley

我想通过php和volley将波斯文本发送到服务器;

发送问号或者,在Android上进行编码时,在mysql表中就像这样:

%D8%AA%D9%86%D9%86%D8%AA

我在Android上使用了URLEncoder.encode

在php中

@mysqli_query($ connect,“SET CHARACTER SET utf8;”);

在mysql中,所有列都以两种模式进行测试:utf8 general ci和persian 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'] ) );

 ?>

2 个答案:

答案 0 :(得分:0)

尝试将表格排名更改为utf8_unicode_ci

答案 1 :(得分:0)

整理与编码无关。

我认为你期待var sortedLists = numberLists.OrderBy(x => string.Join(",", x.ToArray())).ToList(); ?在'تننت'(或CHARACTER SET utf8)中进行编码时,即十六进制utf8mb4(无百分号)。

每个D8AAD986D986D8AA之后你真的有空格吗?

%来自哪里?这真的在桌子上吗?或者是为你显示十六进制的东西。

为什么在%xx

中的BOM后面包含空格?

使用任何类型的编码器/解码器通常会适得其反。

请提供response = response.replaceFirst(" ", "");,以便我们查看该表格是否只有SELECT HEX(col) ...。每个阿拉伯字母编码为2字节代码:D8AAD986D986D8AA