I want to receive from an MySQL-Server all my data from a table. So, I wrote a PHP script that fetches the complete table and encode it to JSON format.
PHP-Code:
<?php
include ("connect.php");
mysql_query("SET NAMES 'utf8'");
$query = mysql_query('SELECT * FROM MyTable);
while ($row = mysql_fetch_assoc($query))
{
$buffer[] = $row;
}
print json_encode( $buffer );
mysql_close($dbconnect);
?>
So, when I run the script, all the JSON String looks fine, no corupted characters etc.
In Android App:
When I run the following codes and receive the JSON String throught the HTTP, I get a strange String in the Debugger...
Screenshot: http://goo.gl/gTssQ
Java Code:
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// HTTP Verbindung
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.com");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Fehler bei der http Verbindung "+e.toString());
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),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("log_tag", "Error converting result "+e.toString());
}
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
json_data = jArray.getJSONObject(i);
);
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
I cant understand the Problem, because my Database is full UTF-8 and my script too. I hope someone can help me! :-) Greets, andr3w
'
in this line:$query = mysql_query('SELECT * FROM MyTable);
. Strange if it is a typo (you didn't cut/paste the code?) but even stranger if it isn't :) – Nanne Oct 9 '11 at 18:03