Search code examples
androidmysqljsonspecial-charactershtmlspecialchars

Android getting data containing special/copy-pasted characters from mysql server


I need to get some data from a mysql database.I'm running a php script querying the database,json encoding and printing it. Getting the results from my application through a httppost on the php in the server.I am getting all the data from the server except for some entries which contain special/non-typed characters.for eg :“,’ For these entries I am getting null.So other than replacing these characters with typed characters like '"' server side...is there any way I can get these data with these characters to show on the application. Thanks.

 try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(KEY_121);
                    //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

            }catch(Exception e){
                    Log.e("cxxn", "Error in http connection "+e.toString());
            }

            //convert response to string
            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();
                    result=sb.toString();
            }catch(Exception e){
                    Log.e("convt", "Error converting result "+e.toString());
            }

The result has null values for those data with special characters.Should I change the encoding that I am using..? Edit: The MySQL charset is: UTF-8 Unicode (utf8)

<?php

      mysql_connect("","","");

      mysql_select_db("fdict");

      $q=mysql_query("SELECT (Id), (WD), (des), (udte) FROM `dict`");

      while($e=mysql_fetch_assoc($q))

              $output[]=$e;

           print(json_encode($output));

    mysql_close();
?>

This is my server side php code.Have removed the server address,pass.The data with special characters seem to become null only after json_encode().An echo of the sql query result shows proper data without any null values.Someone please help with this.What can i do..?What are my options..?

Thanks.


Solution

  • Solved it Using this. Needed to put mysql_query('SET CHARACTER SET utf8') before the SELECT query.So that it gets retrived in utf8 format.