Tuesday, February 11, 2014

json on the client and server side Chuan Chinese garbled



Json
client package
 
HttpClient client = new DefaultHttpClient ();
HttpPost post = new HttpPost (strUrl);
StringEntity entity = new StringEntity (json);
entity.setContentType ("application / json");
entity.setContentEncoding ("utf-8");
post.setEntity (entity);
HttpResponse responString = client.execute (post);


The server accepts data:
 
try {
BufferedReader br = new BufferedReader (new InputStreamReader ((ServletInputStream) request.getInputStream ()));
String line = "";

StringBuilder sb = new StringBuilder ();
while ((line = br.readLine ())! = null) {
sb.append (line);
}
System.out.println ("diaryjson:" + sb.toString ());
} Catch (IOException e) {
/ / TODO Auto-generated catch block
System.out.println ("diaryjson receive failure!!!!");
}


There are Chinese data transfer, the server-side access to the garbled, how to solve? !
Reply:

Before doing jsp is solvedString str = new String (request.getParameter ("something") getBytes ("ISO-8859-1"), "utf-8".);

Should be able to refer to the
Reply:
cited a floor guoyoulei520 reply:
before doing jsp is solved
String str = new String (request.getParameter ("something") getBytes ("ISO-8859-1"), "utf-8".);

Should be able to refer to the


getBytes become ("ISO-8859-1"), at least you know you pass over the ISO-8859-1 encoding, ah, before it can be converted, and I do not know what to pass over coding, and obviously have been set up in the client utf-8 a. Your method does not work in my die, ah,,,,
Reply:
Returns the result string in the encoding process to try to do something too:
 
System.out.println ("diaryjson:" + URLDecoder.decode (sb.toString (), "utf-8"));

Reply:
reference to the third floor yxinf reply:
results returned string also try to do some coding:
 
System.out.println ("diaryjson:" + URLDecoder.decode (sb.toString (), "utf-8"));


To send data:
After your statement added:

Plus without all the effects! ! ! Great God who can solve the small brother doubts ah! ! ! ! ! ! ! ! !

Reply:
URLDecoder.decode is decoded to.
LZ data transmitted with the encoded URLEncoder.encode, when receiving the decoded then URLDecoder.decode
Reply:
references, 5th Floor a1150247879 reply:
URLDecoder.decode is decoded to.
LZ data transmitted with the encoded URLEncoder.encode, then when receiving the decoding URLDecoder.decode


These methods online that I have tried! ! ! You said the way I have tried! ! !
This is the way to generate the final return json is what you said.
 
public String MakeDiaryJson (String title, String context, String createtime, String lastmodifytime, int user_id) throws Exception {
String diaryjson = "";
JSONObject json = new JSONObject ();
try {
Diary title
/ / diary of depositjson.put ("title", title);
Diary
/ / diary of depositjson.put ("context", context);
/ / Diary diary of deposit creation time
json.put ("createtime", createtime);
The last time / / diary of deposit diary Modified
json.put ("lastmodifytime", lastmodifytime);
Diary of a foreign key into the diary, that this diary belongs to which user
/ /json.put ("user_id", user_id);
diaryjson = json.toString ();
URLDecoder.decode (diaryjson, "utf-8");
} Catch (JSONException e) {
/ / TODO Auto-generated catch block
Log.i ("JSonInfo:", "!!!! MakeDiaryJson create failure");
}
return URLDecoder.decode (diaryjson, "utf-8");
};

Server code:
 
public String execute () throws Exception {
/ / TODO Auto-generated method stub
System.out.println ("execute () method was executed!!!");
try {
BufferedReader br = new BufferedReader (new InputStreamReader ((ServletInputStream) request.getInputStream ()));

String line = "";
StringBuilder sb = new StringBuilder ();
while ((line = br.readLine ())! = null) {
sb.append (line);
}
System.out.println ("diaryjson:" + URLDecoder.decode (sb.toString (), "utf-8"));

} Catch (IOException e) {
/ / TODO Auto-generated catch block
System.out.println ("diaryjson receive failure!!!!");

}

Reply:
reference to the 6th floor glwang520 reply:
Quote: references, 5th Floor a1150247879 reply:

URLDecoder.decode is decoded to.
LZ data transmitted with the encoded URLEncoder.encode, then when receiving the decoding URLDecoder.decode


These methods online that I have tried! ! ! You said the way I have tried! ! !
This is the way to generate the final return json is what you said.
 
public String MakeDiaryJson (String title, String context, String createtime, String lastmodifytime, int user_id) throws Exception {
String diaryjson = "";
JSONObject json = new JSONObject ();
try {
Diary title
/ / diary of depositjson.put ("title", title);
Diary
/ / diary of depositjson.put ("context", context);
/ / Diary diary of deposit creation time
json.put ("createtime", createtime);
The last time / / diary of deposit diary Modified
json.put ("lastmodifytime", lastmodifytime);
Diary of a foreign key into the diary, that this diary belongs to which user
/ /json.put ("user_id", user_id);
diaryjson = json.toString ();
URLDecoder.decode (diaryjson, "utf-8");
} Catch (JSONException e) {
/ / TODO Auto-generated catch block
Log.i ("JSonInfo:", "!!!! MakeDiaryJson create failure");
}
return URLDecoder.decode (diaryjson, "utf-8");
};

Server code:
 
public String execute () throws Exception {
/ / TODO Auto-generated method stub
System.out.println ("execute () method was executed!!!");
try {
BufferedReader br = new BufferedReader (new InputStreamReader ((ServletInputStream) request.getInputStream ()));

String line = "";
StringBuilder sb = new StringBuilder ();
while ((line = br.readLine ())! = null) {
sb.append (line);
}
System.out.println ("diaryjson:" + URLDecoder.decode (sb.toString (), "utf-8"));

} Catch (IOException e) {
/ / TODO Auto-generated catch block
System.out.println ("diaryjson receive failure!!!!");

}



return URLDecoder.decode (diaryjson, "utf-8") ;/ / decoding code phrase is used to send coded

Reply:
reference to the 7th floor a1150247879 reply:
Quote: references to the 6th floor glwang520 reply:

Quote: references, 5th Floor a1150247879 reply:

URLDecoder.decode is decoded to.
LZ data transmitted with the encoded URLEncoder.encode, then when receiving the decoding URLDecoder.decode


These methods online that I have tried! ! ! You said the way I have tried! ! !
This is the way to generate the final return json is what you said.
 
public String MakeDiaryJson (String title, String context, String createtime, String lastmodifytime, int user_id) throws Exception {
String diaryjson = "";
JSONObject json = new JSONObject ();
try {
Diary title
/ / diary of depositjson.put ("title", title);
Diary
/ / diary of depositjson.put ("context", context);
/ / Diary diary of deposit creation time
json.put ("createtime", createtime);
The last time / / diary of deposit diary Modified
json.put ("lastmodifytime", lastmodifytime);
Diary of a foreign key into the diary, that this diary belongs to which user
/ /json.put ("user_id", user_id);
diaryjson = json.toString ();
URLDecoder.decode (diaryjson, "utf-8");
} Catch (JSONException e) {
/ / TODO Auto-generated catch block
Log.i ("JSonInfo:", "!!!! MakeDiaryJson create failure");
}
return URLDecoder.decode (diaryjson, "utf-8");
};

Server code:
 
public String execute () throws Exception {
/ / TODO Auto-generated method stub
System.out.println ("execute () method was executed!!!");
try {
BufferedReader br = new BufferedReader (new InputStreamReader ((ServletInputStream) request.getInputStream ()));

String line = "";
StringBuilder sb = new StringBuilder ();
while ((line = br.readLine ())! = null) {
sb.append (line);
}
System.out.println ("diaryjson:" + URLDecoder.decode (sb.toString (), "utf-8"));

} Catch (IOException e) {
/ / TODO Auto-generated catch block
System.out.println ("diaryjson receive failure!!!!");

}



return URLDecoder.decode (diaryjson, "utf-8") ;/ / decoding code phrase is used to send coded




What coding? Is encodeuricomponent ()?
Reply:
URLEncoder and URLDecoder java class in encoding and decoding processing problems

Code: URLEncoder.encode (origialStr, "UTF-8");
Decoding: URLDecoder.decode (sb.toString (), "utf-8");
Reply:
StringEntity rentity = new StringEntity (jsonStr, "UTF-8"); so try it.

No comments:

Post a Comment