-1

So I was looking on StackOverflow for the answer on this question but couldn't find one.

I have an URL whose left and right part are always the same, but the part in a middle changes constantly. If there's only one word it works fine, but if there are multiple the URL brokes.

I need to replace all the whitespace with plus (+) sign.

I'm using this code with no luck:

String content = bioObj.getString("summary");
                    content = content.replaceAll(" ", "%20");
                    bio.setText(content);

Can anyone help?

FULL CODE:

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);

ERROR:

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
DaxHR
  • 683
  • 1
  • 11
  • 30

1 Answers1

1

I would just use the replace method. Using your 'content' string:

content.replace(' ', '+');

This will check for a space and replace the space with a plus. If this is a problem when using your entire string due to the beginning and end I would simply substring the middle and then use the replace method. Try https://docs.oracle.com/javase/7/docs/api/java/lang/String.html for all references to a string in java and their predefined references.

Austin Efnor
  • 166
  • 1
  • 2
  • 16