4

One part of my app performs a query (via php) on a mysql database. I use in the database UTF-8 because I have letters like é à ê that need to appear. I read through this problem because this seemed almost the same.

Android Java UTF-8 HttpClient Problem

However when I am implementing the code he replaces every return value with an é as null.

This is my code

HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        params.setBooleanParameter("http.protocol.expect-continue", false);
        HttpClient httpclient = new DefaultHttpClient(params);

             HttpPost httppost = new HttpPost("http://www.example.com/example.php");
             httppost.setEntity(new UrlEncodedFormEntity(query));
             HttpResponse response = httpclient.execute(httppost);


             HttpEntity entity = response.getEntity();
             String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
             Toast.makeText(StoresInfo.this, jsonText, Toast.LENGTH_LONG).show();
             is = entity.getContent();

So in the jsonText string he replaces return values with a "è" in it by null.

The last line is = entity.getConent(); I added this because I normally use the input stream to read it but this does not work as well.

Somebody has an idea?

This is my php code

<?php
     mysql_select_db("database");

        $q=mysql_query($_REQUEST['query']);

          while($e=mysql_fetch_assoc($q))

                $output[]=$e;
          print(json_encode($output));
    mysql_close();


    ?>
Community
  • 1
  • 1
user661434
  • 45
  • 1
  • 5
  • Are you sure your web interface is in UTF-8? – Hrk Mar 15 '11 at 21:53
  • What do you mean? This is my php code... – user661434 Mar 15 '11 at 21:57
  • Are you sur your PHP interface you're asking is providing you UTF-8 content and not another encoding ? – Hrk Mar 15 '11 at 22:01
  • In mysql I am using a varchar with utf8_bin. – user661434 Mar 15 '11 at 22:01
  • I tried to output it by echoing it and it showed me the letters yes... If this is an answer to your question :-) – user661434 Mar 15 '11 at 22:03
  • Try to check in your browser which encoding is used to decode your content. Your browser will adapt automatically depending on the encoding found. – Hrk Mar 15 '11 at 22:05
  • Owkay, and how can i find this out... I am using firefox – user661434 Mar 15 '11 at 22:11
  • hmm ok, sorry, that should be correct – Hrk Mar 15 '11 at 22:12
  • idd. Could it be a problem in my php code? – user661434 Mar 15 '11 at 22:13
  • I don't think if you browser tells you it's unicode, but the java code above seems correct, too. Could you give me a quick access to your webpage to test it ? – Hrk Mar 15 '11 at 22:20
  • I've got a null message when I'm on your page, I think I need post params ? – Hrk Mar 15 '11 at 22:26
  • yes. Because the page is only to be read by a mobile phone... There is actually nothing on it. It will just perform the query... The code of the php page is above... – user661434 Mar 15 '11 at 22:27
  • But if you have a null answer from your server, it's normal to have a null answer on android device, no ?? – Hrk Mar 15 '11 at 22:40
  • I don't think so. My query returns several fields, but he cannot return the fields with a "é". In my application one query is performed. This fills a list with several names, just the ones with an é or è in it show null – user661434 Mar 15 '11 at 22:41
  • I try to go on your website directly with my browser, I got a null message. I try to go on your website with my mobile phone, I got a null message. I try to go on your website with my mobile phone through the code you write above, I got a null message. I don't really understand what you expect. – Hrk Mar 15 '11 at 22:44
  • I did not poste the complete code... httppost.setEntity(new UrlEncodedFormEntity(query)); in this query field in the app you have something like Select * From Table... This is fild in in the php line: $q=mysql_query($_REQUEST['query']); It is consequently logic that you do not see a thing on your mobile phone either. You do need to have my whole application for that. – user661434 Mar 15 '11 at 22:46
  • What should be your "query" variable ? – Hrk Mar 15 '11 at 22:48
  • query variable = "SELECT * FROM "+sinfo+" WHERE (Longitude>='"+longlow+"' AND Longitude<='"+longhigh+"' AND Latitude>='"+latlow+"' AND Latitude<='"+lathigh+"' )")) with sinfo the name of the table – user661434 Mar 15 '11 at 22:50
  • This query is copied into the php page... – user661434 Mar 15 '11 at 22:51
  • so if you run the php page without that query he will return null – user661434 Mar 15 '11 at 22:51
  • I want try to reproduce the problem with your code. Could you give me a full query working. – Hrk Mar 15 '11 at 22:54
  • I just checked your personal website. It would not be a good idea to send you this information – user661434 Mar 15 '11 at 22:59
  • ok, send me an email to REMOVED. I don't have battery anymore, I think I will have a quick look at your problem tomorrow morning. – Hrk Mar 15 '11 at 23:00
  • But thank you for your help anyway! – user661434 Mar 15 '11 at 23:00
  • Ok, no problem if you don't want my help. All i wanted was to help you. Because I already develop this kind of interface (not with PHP) and it works for me. – Hrk Mar 15 '11 at 23:02
  • Yeah I know, but I can not give you the table names, nor the info inside. Sorry mate. – user661434 Mar 15 '11 at 23:03

1 Answers1

5

After trying your code on another page, I think your problem come from your server web interface.

You can for example try a GET request on a google page containing "é" characters:

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
params.setBooleanParameter("http.protocol.expect-continue", false);
HttpClient httpclient = new DefaultHttpClient(params);

HttpGet httpget = new HttpGet("http://www.google.ch/search?sourceid=chrome&ie=UTF-8&q=www.google.frb%C3%A9b%C3%A9&qscrl=1#sclient=psy&hl=fr&qscrl=1&source=hp&q=b%C3%A9b%C3%A9&aq=&aqi=&aql=f&oq=&pbx=1&fp=b4d89b2783e136eb&pf=p&pdl=300");

    try
    {
         HttpResponse response = httpclient.execute(httpget);

         HttpEntity entity = response.getEntity();
         String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
         Log.d("TEST", jsonText);
         Toast.makeText(this, jsonText, Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {
    }
Hrk
  • 2,725
  • 5
  • 29
  • 44
  • Hi mate! This actually did the trick! I wrote everything in a comma separted file in notepad and uploaded it! Now i see the accents! Thank you very very much! – user661434 Mar 16 '11 at 17:12