I get the answer by debug restlet.
The problem is caused by "CharSequence".
In Java,CharSequence default uses "unicode".
In my code:
request.getEntityAsForm().getValues(JSON_PARAM);
This statement will call construct Form Object by posted request
data,and parse parameters from it.

In FormUtils class,there is a parameter parse method:
    public static Parameter create(CharSequence name, CharSequence value,
            CharacterSet characterSet) throws IOException {
        Parameter result = null;
        if (name != null) {
            if (value != null) {
                //notice:result is encoded by unicode.So when we do
next step,we get wrong converted result.
                result = new Parameter(Reference.decode(name.toString(),
                        characterSet), Reference.decode(value.toString(),
                        characterSet));
            } else {
                result = new Parameter(Reference.decode(name.toString(),
                        characterSet), null);
            }
        }

        return result;
    }

So I add a pre-convert code:
...
        if (name != null) {
            if (value != null) {
                //added statement:it will convert unicode to
corresponding characterset(here is utf-8).
                value=new
String(value.toString().getBytes(CharacterSet.ISO_8859_1.getName()),characterSet.getName());
                result = new Parameter(Reference.decode(name.toString(),
                        characterSet), Reference.decode(value.toString(),
                        characterSet));
            } else {
                result = new Parameter(Reference.decode(name.toString(),
                        characterSet), null);
            }
        }
...

Ok,I found opensource is the best way for debug!
Any suggestiong for me?
On Jan 23, 2008 2:33 PM, cleverpig <[EMAIL PROTECTED]> wrote:
>
> I meet a problem on request paramter decode in Chinese enviroment:
> I posted data with UTF-8 code,the data includes some Chinese Characters.
> And then I revieved them from Restlet,the data was changed by wrong
> characterset(maybe it is ISO-8859-1,but UTF-8 is correct):
>
> //This class processes request and give response in JSON way.
> public class AddressbookBean extends Restlet{
>        private static final String ACTION_PARAM="action";
>        private static final String JSON_PARAM="json";
>        private AddressbookService service;
>        private Log log=LogFactory.getLog(AddressbookBean.class);
>
>        @Override
>        public void handle(Request request, Response response) {
>
> //              request.getEntity().setCharacterSet(CharacterSet.UTF_8); 
> //This
> statement don't work for Form,only works for Entity Object
>                String
> actionParamVal=(String)request.getAttributes().get(ACTION_PARAM);
>                log.info(ACTION_PARAM+"="+actionParamVal);
>
>                String 
> jsonParamVal=request.getEntityAsForm().getValues(JSON_PARAM);
>                log.info(JSON_PARAM+"="+jsonParamVal);
> //if I post json param:json={"city":"北京","street":"1","zip":"1","tel":"1"}
> //I will get a wrong json
> param:json={"city":"北京","street":"1","zip":"1","tel":"1"}
>
>                String dbFileDir=null;
>                dbFileDir="/";
>                log.info("DBFileDir="+dbFileDir);
>
>                String responseText=Utils.callMethodAndGainResponseJSONStr(
>                                service,
>                                actionParamVal,
>                                jsonParamVal,
>                                dbFileDir,
>                                Addressbook.class);
>                Representation responseRep = new StringRepresentation(
>                                responseText,
>                                MediaType.APPLICATION_JSON,
>                    Language.ALL,
>                    CharacterSet.UTF_8);
>                response.setEntity(responseRep);
>        }
>
>        public void setService(AddressbookService service ){
>                this.service = service;
>        }
> }
>
> So I checked source code and track to find reason,but i couldn't find
> a function to post customized characterset with Form Object.
>
> What's wrong with my code?After I added these hard-decode,I got correct 
> result:
> String jsonParamVal=request.getEntityAsForm().getValues(JSON_PARAM);
>                 try {
>                         if (jsonParamVal!=null)
>                                 jsonParamVal=new 
> String(jsonParamVal.getBytes("iso8859-1"),"utf-8");
>                 } catch (UnsupportedEncodingException e) {
>                         // TODO Auto-generated catch block
>                         e.printStackTrace();
>                 }
>                 log.info(JSON_PARAM+"="+jsonParamVal);
>
> I found my post body was encode by iso8859-1,but it wasn't my responsibility.I
> don't what is done in restlet.
>
>



-- 
cleverpig
Location: Beijing
Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China
Zipcode: 100031
Phone: 010-66415588-1113
MSN: [EMAIL PROTECTED]
QQ: 149291732
Skepe: cleverpigatmatrix
My Facebook ID:cleverpig
My Blog: hihere.sohu.com
My Tags: del.icio.us/cleverpig
My Twitter: twitter.com/cleverpig
My Organization: www.beijing-open-party.org
一些值得关注的唧歪:
  http://jiwai.de/t/jmatrix/
  http://jiwai.de/t/db4o/
  http://jiwai.de/t/matrix-community/

Reply via email to