when send your string to server
String encodedString = URLEncoder.encode(yourString, "UTF-8");
when get your string from server
String decodedString = URLDecoder.decode(yourString, "UTF-8");
Question
when sending a request on web service from android, it become question marks(?) on web side.
{CategoryId=5; CategoryName=開胃; CategoryDesc = 開胃; }
but in app it display like 開,胃
Solution
when send your string to server
String encodedString = URLEncoder.encode(yourString, "UTF-8");
when get your string from server
String decodedString = URLDecoder.decode(yourString, "UTF-8");
OTHER TIPS
I'm answering bit late but hope it will help someone else. I faced the same issue and resolved by setting CharacterSet
to utf-8
and Content-Type
to application/json
in my case. You can select other content types as per your requirement. See below
StringEntity s = new StringEntity(data.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
Below is the complete method.
public static String doPost(JSONObject data, String url)
throws ClientProtocolException, IOException, TimeoutException, ApiException {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 30000; // 30 Seconds
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 50000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Authorization", "Basic " + addAuthHeader());
Log.d("TAG", "Data ==> " + data.toString());
StringEntity s = new StringEntity(data.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(s);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String status = getASCIIContentFromEntity(entity);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
status = "ApiError";
throw new ApiException();
}
Log.d("TAG", "Response String ====== " + status);
return status;
}