因此,我在StackOverflow上尋找關於此問題的答案,但找不到一個。Java - 用+符號替換字符串中的所有空格用於URL
我有一個左右部分總是相同的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
string.replace(「」,「+」) 這是將字符串中的所有空格轉換爲+號的基本 –
URLEncoder.encode(String)就是我要這樣做的方式。 –
什麼不適合你的嘗試?輸出是什麼,或者例外? – Tunaki