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.

Reply via email to