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 ?
-
If you test it in a browser does it render ok?reactivemobile– reactivemobile2014年10月24日 11:38:49 +00:00Commented Oct 24, 2014 at 11:38
-
No, even when I try the link in a browser it gives me the same result : [{"Taskid":"65","Username":"moonwalker","Title":"Payment","Type1":"Payment","Type":"Text","Content":"i have to pay for my visa to be ready for travel and enjoy the trip","Date":"2014年2月5日","Time":"21:30"},{"Taskid":"60","Username":"moonwalker","Title":"??????? ?? ??????","Type1":"Appreciation","Type":"Voice","Content":"??? ????? ???????? ??????? ?? ???????","Date":"2014年02月05日","Time":"19:48"}]Monzer Yaghi– Monzer Yaghi2014年10月24日 11:38:59 +00:00Commented Oct 24, 2014 at 11:38
-
OK so the problem is with the database, you should probably repost the question with different tagsreactivemobile– reactivemobile2014年10月24日 11:41:10 +00:00Commented Oct 24, 2014 at 11:41
-
Ok and do you have a solution or can you help me ?Monzer Yaghi– Monzer Yaghi2014年10月24日 11:42:38 +00:00Commented Oct 24, 2014 at 11:42
-
1It's not my area but this is taken from another answer (stackoverflow.com/questions/18428417/…): $BD->query("SET NAMES 'utf8'"); $BD->query("SET CHARACTER SET utf8");reactivemobile– reactivemobile2014年10月24日 11:46:28 +00:00Commented Oct 24, 2014 at 11:46
1 Answer 1
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