Skip to content

Commit

Permalink
Ensure that all data is sent over as UTF-8
Browse files Browse the repository at this point in the history
On android, unless we explicitly serialize the data as UTF-8, it is serialized as ISO-8851.
On iOS, it is serialized as UTF-8 by default.

In both, it is probably best to explicitly mark the content as UTF-8 in the HTTP header

Bump up the version number to match

This fixes e-mission/e-mission-docs#333

Testing done:
- on android: set the locale to French, set the date to Fevr and synced data
    - before this change, it did not work
    - after this change, it works
- on iOS, set the locale to French, set the date to Fevr and synced data
    - the formatted data did not have any words so even before this fix, it worked
    - this did not cause a regression
  • Loading branch information
shankari committed May 9, 2019
1 parent 24309f8 commit 92f3cf8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "em-cordova-server-communication",
"version": "1.0.1",
"description": "Simple package that stores all the connection settings that need to be configured",
"version": "1.2.1",
"description": "Package that handles communication with the server",
"cordova": {
"id": "em-cordova-server-communication",
"platforms": [
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="edu.berkeley.eecs.emission.cordova.comm"
version="1.2.0">
version="1.2.1">

<name>ServerComm</name>
<description>Abstraction for communication settings, and for making both GET
Expand Down
15 changes: 9 additions & 6 deletions src/android/CommunicationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ public static String pushGetJSON(Context ctxt, String fullURL,
String result = "";
HttpPost msg = new HttpPost(fullURL);
System.out.println("Posting data to " + msg.getURI());
msg.setHeader("Content-Type", "application/json");
msg.setHeader("Content-Type", "application/json; charset=UTF-8");

// Fill in the object
final String userToken = CommunicationHelper.getTokenSync(ctxt);
filledJsonObject.put("user", userToken);
msg.setEntity(new StringEntity(filledJsonObject.toString()));
StringEntity se = new StringEntity(filledJsonObject.toString(), "UTF-8");
msg.setEntity(se);

// Perform the operation
AndroidHttpClient connection = AndroidHttpClient.newInstance(ctxt.getString(R.string.app_name));
Expand Down Expand Up @@ -121,12 +122,13 @@ public static void pushJSON(Context ctxt, String fullURL, String userToken,
throws IOException, JSONException {
HttpPost msg = new HttpPost(fullURL);
System.out.println("Posting data to " + msg.getURI());
msg.setHeader("Content-Type", "application/json");
msg.setHeader("Content-Type", "application/json; charset=UTF-8");
JSONObject toPush = new JSONObject();

toPush.put("user", userToken);
toPush.put(objectLabel, jsonObjectOrArray);
msg.setEntity(new StringEntity(toPush.toString()));
StringEntity se = new StringEntity(toPush.toString(), "UTF-8");
msg.setEntity(se);
AndroidHttpClient connection = AndroidHttpClient.newInstance(ctxt.getString(R.string.app_name));
HttpResponse response = connection.execute(msg);
System.out.println("Got response " + response + " with status " + response.getStatusLine());
Expand All @@ -142,12 +144,13 @@ public static String getUserPersonalData(Context ctxt, String fullURL, String us
JSONException, IOException {
String result = "";
HttpPost msg = new HttpPost(fullURL);
msg.setHeader("Content-Type", "application/json");
msg.setHeader("Content-Type", "application/json; charset=UTF-8");

//String result;
JSONObject toPush = new JSONObject();
toPush.put("user", userToken);
msg.setEntity(new StringEntity(toPush.toString()));
StringEntity se = new StringEntity(toPush.toString(), "UTF-8");
msg.setEntity(se);

System.out.println("Posting data to "+msg.getURI());

Expand Down
3 changes: 2 additions & 1 deletion src/ios/BEMCommunicationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ - (void)postToHost:(NSString*)idToken {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:self.mUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:500];
// UTF-8 fix from https://stackoverflow.com/questions/28229616/how-to-properly-encode-utf-8-in-ios-using-nsmutableurlrequest
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json"
[request setValue:@"application/json; charset=UTF-8"
forHTTPHeaderField:@"Content-Type"];

NSString *userToken = idToken;
Expand Down

0 comments on commit 92f3cf8

Please sign in to comment.