[英]Java - replace all blank space in String with + sign for URL
所以我在 StackOverflow 上寻找这个问题的答案,但找不到。
我有一个 URL,它的左右部分始终相同,但中间部分不断变化。 如果只有一个字,它工作正常,但如果有多个 URL 坏了。
我需要用加号 (+) 替换所有空格。
我使用这段代码没有运气:
String content = bioObj.getString("summary");
content = content.replaceAll(" ", "%20");
bio.setText(content);
谁能帮忙?
完整代码:
String finalUrlBio = bioUrlLeft+title+bioUrlRight;
JsonObjectRequest bioRequest = new JsonObjectRequest(Request.Method.GET, finalUrlBio, (JSONObject) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject artistObj = response.getJSONObject("artist");
JSONObject bioObj = artistObj.getJSONObject("bio");
String content = bioObj.getString("summary");
content = content.replaceAll("\\s","+");
bio.setText(content);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(bioRequest);
错误:
07-01 00:00:37.379 2793-2872/com.darioradecic.topmusicandartists E/Volley: [397649] BasicNetwork.performRequest: Unexpected response code 400 for http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Red Hot Chili Peppers&api_key=99b9ea4e41463638c76b4759d8b1da1d&format=json
我只会使用替换方法。 使用您的“内容”字符串:
content.replace(' ', '+');
这将检查空格并将空格替换为加号。 如果由于开头和结尾而在使用整个字符串时出现问题,我会简单地将 substring 放在中间,然后使用替换方法。 尝试https://docs.oracle.com/javase/7/docs/api/java/lang/String.html以获取对 java 中字符串的所有引用及其预定义引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.