且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的Java的UTF-8 JSON

更新时间:2023-11-27 12:31:46

尝试其他页面上的code后,我想从你的服务器的Web界面来你的问题。

您可以尝试例如含有é字符的谷歌网页上的一个GET请求:

 的HttpParams PARAMS =新BasicHttpParams();
HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(参数,可以UTF-8);
params.setBooleanParameter(http.protocol.expect-继续,FALSE);
HttpClient的HttpClient的=新DefaultHttpClient(PARAMS);HTTPGET HTTPGET =新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\");    尝试
    {
         HTT presponse响应= httpclient.execute(HTTPGET);         HttpEntity实体= response.getEntity();
         字符串jsonText = EntityUtils.toString(实体,HTTP.UTF_8);
         Log.d(TEST,jsonText);
         Toast.makeText(这一点,jsonText,Toast.LENGTH_LONG).show();
    }
    赶上(例外五)
    {
    }

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();


    ?>

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)
    {
    }