Hi,Rob.I made a pair of test program that can prove my consideration,pls try on:
description:
client post a json param to server (uri=/params),server will display
params on the console.
client:
public class ZhProblemClient {
public static void main(String[] argv) throws IOException{
// Prepare the request
Request request = new Request(Method.POST,
"http://localhost:8182/params");
request.setReferrerRef("http://www.bjinfotech.com");
request.getClientInfo().getAcceptedCharacterSets().add(new
Preference<CharacterSet>(CharacterSet.ALL));
request.getClientInfo().getAcceptedEncodings().add(new
Preference<Encoding>(Encoding.GZIP));
request.getClientInfo().getAcceptedLanguages().add(new
Preference<Language>(Language.ALL));
request.getClientInfo().getAcceptedMediaTypes().add(new
Preference<MediaType>(MediaType.APPLICATION_JAVASCRIPT));
request.getClientInfo().getAcceptedMediaTypes().add(new
Preference<MediaType>(MediaType.APPLICATION_XML));
request.getClientInfo().getAcceptedMediaTypes().add(new
Preference<MediaType>(MediaType.TEXT_HTML));
request.getClientInfo().getAcceptedMediaTypes().add(new
Preference<MediaType>(MediaType.TEXT_XML));
request.getClientInfo().getAcceptedMediaTypes().add(new
Preference<MediaType>(MediaType.ALL));
// my json
parameter:json={"zip":1,"tel":"1","street":"1","city":"北京"}
StringRepresentation jsonRepr=new StringRepresentation(
"json={\"zip\":1,\"tel\":\"1\",\"street\":\"1\",\"city\":\"北京\"}",
MediaType.APPLICATION_JSON,
Language.ALL,
CharacterSet.UTF_8);
request.setEntity(jsonRepr);
// Handle it using an HTTP client connector
Client client = new Client(Protocol.HTTP);
Response response = client.handle(request);
System.out.println(response.getEntity().getCharacterSet().getName());
// Write the response entity on the console
Representation output = response.getEntity();
output.setCharacterSet(CharacterSet.UTF_8);
// I got wrong response:<html><body>json:
{"zip":1,"tel":"1","street":"1","city":"å京"}<br/></body></html>
// 北京==>å京(this response is a unicode or iso-8859-1 encoded)
System.out.println(output.getText());
}
}
server:
public class ZhProblemApplication {
public static void main(String[] argv) throws Exception{
Component component=new Component();
component.getServers().add(Protocol.HTTP,8182);
component.getClients().add(Protocol.FILE);
Application application=new Application(component.getContext()){
@Override
public Restlet createRoot(){
Router router=new Router(getContext());
router.attach("/zhform", new
Restlet(getContext()){
@Override
public void handle(Request request, Response
response){
response.setEntity("<html><body><form
method='post'
action='zhproc'>"
+"city: <input type='text' size='40'
name='test1'/><br/>"
+"street <input type='text' size='40'
name='test2'/><br/>"
+"<input type='submit'/>"
+"</form></body></html>",
MediaType.TEXT_HTML);
response.getEntity().setCharacterSet(CharacterSet.valueOf("GBK"));
}
});
router.attach("/zhproc", new
Restlet(getContext()){
@Override
public void handle(Request request, Response
response){
CharacterSet csin =
request.getEntity().getCharacterSet();
if(csin!=null)
System.out.println (">>> Incoming character
set
is "+csin.getName());
else
System.out.println(">>> No incoming
character
set detected");
// Force to the charset we are actually
receiving.
If I set this *wrong* I get
// gibberish
request.getEntity().setCharacterSet(CharacterSet.valueOf("GBK"));
System.out.println(">>> Character set reset to
"+request.getEntity().getCharacterSet().getName());
Form f = new Form(request.getEntity());
response.setEntity("<html><body>"
+"city:
"+f.getFirstValue("test1")+"<br/>"
+"street:
"+f.getFirstValue("test2")+"<br/>"
+"</body></html>",MediaType.TEXT_HTML);
// I can set any output encoding and it appears
to render OK
response.getEntity().setCharacterSet(CharacterSet.valueOf("GBK"));
}
});
router.attach("/params", new
Restlet(getContext()){
@Override
public void handle(Request request, Response
response){
CharacterSet csin =
request.getEntity().getCharacterSet();
if(csin!=null)
System.out.println (">>> Incoming character
set
is "+csin.getName());
else
System.out.println(">>> No incoming
character
set detected");
System.out.println(">>> Character set reset to
"+request.getEntity().getCharacterSet().getName());
Form f = new Form(request.getEntity());
response.setEntity("<html><body>"
+"json: "+f.getValues("json")+"<br/>"
+"</body></html>",MediaType.TEXT_HTML);
response.getEntity().setCharacterSet(CharacterSet.UTF_8);
}
});
return router;
}
};
component.getDefaultHost().attach(application);
component.start();
}
}
On Jan 24, 2008 8:30 PM, Rob Heittman <[EMAIL PROTECTED]> wrote:
>
> Hmmm. if that example works for you, I'd guess the core Restlet request and
> response character set handling is doing what it's supposed to.
>
> I've never exercised JSON with double byte character sets ... maybe there is
> something funny about character set handling on that code path specifically.
> I will construct some more examples to see if I can reproduce what you're
> experiencing, but if anybody else knows more about this already, please
> weigh in!
>
>
>
> On 1/24/08, cleverpig <[EMAIL PROTECTED]> wrote:
> > I tried your code,it's fine.
> > But that's form post and page display function,not a json interface.
> > When I switched to json commute way,I got same wrong..
> >
>
--
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/