Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Json encoding error

Tags:

java

android

I'm developing an android application that connects to a database online to get data from it and populate a ListView. I'm using a php page to get the information, the data is encoded using json. The database contains arabic letters, but they're not encoded well, they're showed as following : "???????".

The java code is :

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

//httpclient = new DefaultHttpClient();
httpclient = new DefaultHttpClient();
String type2="car";
httppost = new HttpPost("http://www.mypage.com/myfolder/mypage.php?Username="+type2+"");

try
{
    nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("Type",type));
    nameValuePairs.add(new BasicNameValuePair("Date",date));
    nameValuePairs.add(new BasicNameValuePair("Title",title));
    nameValuePairs.add(new BasicNameValuePair("Content",content));
    nameValuePairs.add(new BasicNameValuePair("Time",time));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
    response = httpclient.execute(httppost);

    if(response.getStatusLine().getStatusCode()==200)
    {
        entity=response.getEntity();

        if(entity !=null)
        {
            InputStream instream=entity.getContent();

            try
            {
                JSONArray jArray = new JSONArray(convertStreamToString(instream));
arr=jArray;
                //  int jArrayLength = arr.length();
                postsinarray = new String[arr.length()];
                arrayinlist=new String[arr.length()];

                //final List<String> listContents = new ArrayList<String>(jArrayLength);
                //thelist=listContents;
            }
            catch(JSONException e)
            {

            }
        }
    }
}//End of first try

catch(Exception e)
{
    // Toast.makeText(getBaseContext(),"CONNECTION ERROR",Toast.LENGTH_LONG).show();
}

The php code is :

<?php
//ini_set('default_charset','utf-8');
//header('content-type:text/html;charset=utf-8');

$host="myhost";
$user="myuser";
$pass="mypass";
$dbname="mydb";

$Username=$_GET['Username'];

$con= mysql_connect($host,$user,$pass);
$BD= mysql_select_db($dbname, $con);

$query=mysql_query("select * from Todolist where Username='".$Username."' ORDER BY Date DESC");
$num=mysql_num_rows($query);

//echo phpversion();

if($num>=1)
{
    $output=array();
    while($list=mysql_fetch_assoc($query))
    {
        $output[]=$list;
    }

    echo json_encode($output,JSON_UNESCAPED_UNICODE);
    mysql_close();
}
else
{
    echo "error";
}
?>

Any help please ?

like image 851
Monzer Yaghi Avatar asked Jul 01 '26 01:07

Monzer Yaghi


1 Answers

I think you should use the simple AsyncHttp library.

Steps :

1) Download and keep jar inside your project library folder.

2) Create Instance of AsyncHttp .

3) Provide Url and stay free from being righting a lot of every time.

Working Example of AsyncHttp

like image 194
Danial Hussain Avatar answered Jul 02 '26 16:07

Danial Hussain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!