php-从android通过JSON到mysql阿拉伯语

php-从android通过JSON到mysql阿拉伯语,第1张

概述这是我的Java文件.我正在尝试发送阿拉伯文字母而不是我收到的阿拉伯语单词无效????????????????我可以从MySQL读取阿拉伯语,但不能将阿拉伯语添加到MySQL.publicclassNewSecretextendsActivity{//ProgressDialogprivateProgressDialogpDialog;JSONParserjsonPa

这是我的Java文件.我正在尝试发送阿拉伯文字母而不是我收到的阿拉伯语单词无效???? ?????? ??????

我可以从MySQL读取阿拉伯语,但不能将阿拉伯语添加到MysqL.

public class NewSecret extends Activity {// Progress Dialogprivate ProgressDialog pDialog;JsONParser JsonParser = new JsONParser();EditText inputname;EditText inputDesc;// url to create new productprivate static String url_create_product = "http://laylakaylif.com/androID/add_secrets.PHP";// JsON Node namesprivate static final String TAG_SUCCESS = "success";@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    // fullScreen    requestwindowFeature(Window.FEATURE_NO_Title);    getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN,            WindowManager.LayoutParams.FLAG_FulLSCREEN);    setContentVIEw(R.layout.add);    // Edit Text    inputname = (EditText) findVIEwByID(R.ID.inputname);    inputDesc = (EditText) findVIEwByID(R.ID.inputDesc);    linearLayout ll = (linearLayout) findVIEwByID(R.ID.lina);    AdVIEw ad2 = new AdVIEw(NewSecret.this, AdSize.SMART_BANNER,            "a150b0de6e44a18");    ll.addVIEw(ad2);    ad2.loadAd(new AdRequest());    // Create button    button btnCreateProduct = (button) findVIEwByID(R.ID.btnCreateProduct);    if (inputname.getText().toString().length() <= 0)        inputname.setError("الرجاء اضافة عنوان!");    if (inputDesc.getText().toString().length() <= 10)        inputDesc.setError("الرجاء اضافة نص السر ..");    // button click event    btnCreateProduct.setonClickListener(new VIEw.OnClickListener() {        public voID onClick(VIEw vIEw) {            // creating new product in background thread            new CreateNewProduct().execute();        }    });}/** * Background Async Task to Create new product * */class CreateNewProduct extends AsyncTask<String, String, String> {    /**     * Before starting background thread Show Progress Dialog     * */    @OverrIDe    protected voID onPreExecute() {        super.onPreExecute();        pDialog = new ProgressDialog(NewSecret.this);        pDialog.setMessage("إضافة سر جديد ...");        pDialog.setIndeterminate(false);        pDialog.setCancelable(true);        pDialog.show();    }    /**     * Creating product     * */    protected String doInBackground(String... args) {        String Title = inputname.getText().toString();        String description = inputDesc.getText().toString();        String HTMLTitle;        String desHTML;        HTMLTitle = TextUtils.HTMLEncode(Title);        desHTML = TextUtils.HTMLEncode(description);        // Building Parameters        List<nameValuePair> params = new ArrayList<nameValuePair>();        params.add(new BasicnameValuePair("Title", HTMLTitle));        params.add(new BasicnameValuePair("description", desHTML));        // getting JsON Object        // Note that create product url accepts POST method        JsONObject Json = JsonParser.makehttpRequest(url_create_product,                "POST", params);        // check log cat fro response        Log.d("Create Response", Json.toString());        // check for success tag        try {            int success = Json.getInt(TAG_SUCCESS);            if (success == 1) {                // successfully created product                Intent i = new Intent(getApplicationContext(),                        AllSecrets.class);                startActivity(i);                // closing this screen                finish();            } else {                // Failed to create product            }        } catch (JsONException e) {            e.printstacktrace();        }        return null;    }    /**     * After completing background task dismiss the progress dialog     * **/    protected voID onPostExecute(String file_url) {        // dismiss the dialog once done        pDialog.dismiss();    }}}

这是我的PHP JsON文件:

 <?PHP header("Content-Type:application/Json;charset=utf-8"); //global enCoding since this is the config file -- for arabic support/* * Following code will create a new product row * All product details are read from http Post Request */// array for JsON response$response = array();// check for required fIElds/*if (isset($_POST['Title']) && isset($_POST['description'])) {    $Title = Json_encode($_POST['Title'], JsON_UnesCAPED_UNICODE);    $description = Json_encode($_POST['description'], JsON_UnesCAPED_UNICODE);*/if (isset($_REQUEST['Title']) && isset($_REQUEST['description'])) {    echo $_REQUEST['Title'].'<br/>';/*    $Title = Json_decode($__REQUEST['Title']);    $description = Json_decode($__REQUEST['description']);*/    $Title = $_REQUEST['Title'];    $description = $_REQUEST['description'];    // include db connect class    require_once 'db_connect.PHP';    // connecting to db    $db = new DB_CONNECT();    //setting the connection charset     MysqL_set_charset('utf8',$db);     MysqL_query("SET nameS 'utf8'");     MysqL_query("SET CHaraCTER_SET utf8;");    // MysqL inserting a new row    MysqL_query("SET nameS 'UTF8'");    $result = MysqL_query("INSERT INTO secrets(Title ,  description) VALUES('$Title', '$description')");    // check if row inserted or not    if ($result) {        // successfully inserted into database        $response["success"] = 1;        $response["message"] = "Secret successfully added.";        // echoing JsON response        echo Json_encode($response);    } else {        // Failed to insert row        $response["success"] = 0;        $response["message"] = "Oops! An error occurred.";        // echoing JsON response        echo Json_encode($response);    }} else {    // required fIEld is missing    $response["success"] = 0;    $response["message"] = "required fIEld(s) is missing";    // echoing JsON response    echo Json_encode($response);}?>

JsONParser代码:

import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.io.UnsupportedEnCodingException;import java.util.List;import org.apache.http.httpentity;import org.apache.http.httpResponse;import org.apache.http.nameValuePair;import org.apache.http.clIEnt.ClIEntProtocolException;import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;import org.apache.http.clIEnt.methods.httpGet;import org.apache.http.clIEnt.methods.httpPost;import org.apache.http.clIEnt.utils.URLEncodedUtils;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.Json.JsONException;import org.Json.JsONObject;import androID.util.Log;public class JsONParser {    static inputStream is = null;    static JsONObject jObj = null;    static String Json = "";    // constructor    public JsONParser() {    }    // function get Json from url    // by making http POST or GET method    public JsONObject makehttpRequest(String url, String method,            List<nameValuePair> params) {        // Making http request        try {            // check for request method            if(method == "POST"){                // request method is POST                // defaulthttpClIEnt                DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();                httpPost httpPost = new httpPost(url);                httpPost.setEntity(new UrlEncodedFormEntity(params));                httpResponse httpResponse = httpClIEnt.execute(httpPost);                httpentity httpentity = httpResponse.getEntity();                is = httpentity.getContent();            }else if(method == "GET"){                // request method is GET                DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();                String paramString = URLEncodedUtils.format(params, "utf-8");                url += "?" + paramString;                httpGet httpGet = new httpGet(url);                httpResponse httpResponse = httpClIEnt.execute(httpGet);                httpentity httpentity = httpResponse.getEntity();                is = httpentity.getContent();            }                   } catch (UnsupportedEnCodingException e) {            e.printstacktrace();        } catch (ClIEntProtocolException e) {            e.printstacktrace();        } catch (IOException e) {            e.printstacktrace();        }        try {            BufferedReader reader = new BufferedReader(new inputStreamReader(                    is, "iso-8859-1"), 8);            StringBuilder sb = new StringBuilder();            String line = null;            while ((line = reader.readline()) != null) {                sb.append(line + "\n");            }            is.close();            Json = sb.toString();        } catch (Exception e) {            Log.e("Buffer Error", "Error converting result " + e.toString());        }        // try parse the string to a JsON object        try {            jObj = new JsONObject(Json);        } catch (JsONException e) {            Log.e("JsON Parser", "Error parsing data " + e.toString());        }        // return JsON String        return jObj;    }}

解决方法:

JsONObject Json = JsonParser.makehttpRequest(url_create_product, "POST", params);

我不知道您正在使用什么JsONParser.它不是标准平台的一部分;网路上散布着许多不同名称的类别,其中许多类别包括:

httpPost.setEntity(new UrlEncodedFormEntity(params));

这是一个问题,因为UrlEncodedFormEntity的default编码是ISO-8859-1,而不是UTF-8.如果要传递UTF-8,请将其作为参数include:

httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

如果那不是问题,建议发布JsONParser代码.

总结

以上是内存溢出为你收集整理的php-从android通过JSON到mysql阿拉伯语全部内容,希望文章能够帮你解决php-从android通过JSON到mysql阿拉伯语所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086004.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存