如何通过Android WebView.evaluateJavaScript传递含撇号的字符串至网页
Ah, I've run into this exact issue before! The problem here is that when you use WebView.evaluateJavaScript(), you're essentially passing a string that gets executed as raw JavaScript code. Your attempt to escape the apostrophe with \' works in Java and static JS code, but once it's passed through the evaluate call, that escape gets stripped—leaving you with a syntax-breaking apostrophe in the middle of your JS string literal.
Let me break down why this fails and show you two reliable fixes, with the second being the most robust for translation strings (which often have random special characters).
Why Your Current Approach Fails
When you write:
String text = "What is App\'s name?"; webView.evaluateJavaScript("updateText('" + text + "')", null);
Java interprets \' as a single apostrophe, so the actual string passed to JS becomes:
updateText('What is App's name?')
See the issue? The apostrophe in App's closes the opening single quote early, leaving s name?') as invalid code—hence the "missing ) after argument list" error.
Fix 1: Wrap in Double Quotes (and Escape Internal Double Quotes)
If your translation strings don't have double quotes (or you handle them), you can switch to wrapping the JS argument in double quotes instead of single quotes. Just make sure to escape any double quotes inside your text so they don't break the JS syntax:
String text = "What is App's name?"; // Escape any double quotes in the text for JS String escapedText = text.replace("\"", "\\\""); // Pass the string wrapped in double quotes webView.evaluateJavaScript("updateText(\"" + escapedText + "\")", null);
This generates valid JS:
updateText("What is App's name?")
The apostrophe inside the double-quoted string is totally safe in JS.
Fix 2: Use JSON Serialization (Recommended for Translation Strings)
For translation content that might have any special characters (apostrophes, quotes, newlines, emojis—you name it), JSON serialization is the foolproof way to convert a Java string into a valid JS string literal. It automatically handles all escaping for you, so you don't have to worry about edge cases.
Here's how to do it with Gson (a popular JSON library):
import com.google.gson.Gson; // Your translated string (could come from strings.xml) String translatedText = "What is App's name? And here's a \"quoted\" word!"; Gson gson = new Gson(); // Convert the string to a properly escaped JSON string literal String jsSafeText = gson.toJson(translatedText); // Pass it directly to your JS function webView.evaluateJavaScript("updateText(" + jsSafeText + ")", null);
Gson will turn your string into something like "What is App's name? And here's a \"quoted\" word!", which is a perfectly valid JS string when inserted into the function call.
If you don't want to add a third-party library, you can use Android's built-in JSONObject (though it's a bit more verbose):
import org.json.JSONException; import org.json.JSONObject; try { String translatedText = "What is App's name?"; // Use JSONObject to safely escape the string String jsSafeText = new JSONObject().put("temp", translatedText).getString("temp"); webView.evaluateJavaScript("updateText(" + jsSafeText + ")", null); } catch (JSONException e) { e.printStackTrace(); }
Quick Note on Your JS Function
Make sure your web page has the updateText function defined to receive and display the text:
function updateText(displayText) { // Replace with your actual DOM element ID document.getElementById("text-container").textContent = displayText; }
This approach lets you keep all your translation logic in Android's strings.xml (where it belongs) and safely pass those strings to your WebView without syntax errors.
内容的提问来源于stack exchange,提问作者v.j

