I work with GoogleBook API, when i type one word on EditText, URL link works well for example: I type prince and the app doesn’t crash, but when i type the prince it crashes and give me MalformedURLException : no protocol.
I have searched on Stackoverflow, then i found 2 solutions and nothing solved my problem.
- First Solution:
private String makeUrl(String textFromEditText){ StringBuilder stringBuilder = new StringBuilder(); if (textFromEditText.contains(" ")){ // the method replace the space with %20 textFromEditText = textFromEditText.replace(" ","%20"); } if (textFromEditText != null){ stringBuilder.append("https://www.googleapis.com/books/v1/volumes?download=DOWNLOAD_UNDEFINED&filter=FILTER_UNDEFINED&libraryRestrict=LIBRARY_RESTRICT_UNDEFINED&maxResults=20&orderBy=relevance&printType=ALL&q="); stringBuilder.append(textFromEditText); stringBuilder.append("&key=AIzaSyCJmeSLPw147mRSnc2nZNgPSbebtH-RxDc"); } Log.v("makeURL",stringBuilder.toString()); return stringBuilder.toString(); }
returned value: https://www.googleapis.com/books/v1/volumes?download=DOWNLOAD_UNDEFINED&filter=FILTER_UNDEFINED&libraryRestrict=LIBRARY_RESTRICT_UNDEFINED&maxResults=20&orderBy=relevance&printType=ALL&q=the%20prince&key=AIzaSyCJmeSLPw147mRSnc2nZNgPSbebtH-RxDc (there is %20 between the and prince)
The first If statement above that check if the string contains whitespace didn’t solve my problem, i get the same exception.
- Second Solution:
private String makeUrl(String textFromEditText) { StringBuilder stringBuilder = new StringBuilder(); if (textFromEditText != null){ stringBuilder.append("https://www.googleapis.com/books/v1/volumes?download=DOWNLOAD_UNDEFINED&filter=FILTER_UNDEFINED&libraryRestrict=LIBRARY_RESTRICT_UNDEFINED&maxResults=20&orderBy=relevance&printType=ALL&q="); try{ stringBuilder.append(URLEncoder.encode(textFromEditText,"UTF-8")); Here i used URL encoder }catch (UnsupportedEncodingException io){} stringBuilder.append("&key=AIzaSyCJmeSLPw147mRSnc2nZNgPSbebtH-RxDc"); } Log.v("makeURL",stringBuilder.toString()); return stringBuilder.toString(); }
returned value: https://www.googleapis.com/books/v1/volumes?download=DOWNLOAD_UNDEFINED&filter=FILTER_UNDEFINED&libraryRestrict=LIBRARY_RESTRICT_UNDEFINED&maxResults=20&orderBy=relevance&printType=ALL&q=the+prince&key=AIzaSyCJmeSLPw147mRSnc2nZNgPSbebtH-RxDc (there is + between the and prince)
I pass that value to this method: (The exception occur here)
private static URL createURL(String link) { URL url = null; try{ url = new URL(link); }catch (MalformedURLException mal){ mal.printStackTrace(); } return url; }
Important Update: I typed another word (this: the price) and the app worked well, the problem with some words
Thanks in advance
Advertisement
Answer
Thanks to everyone, the problem was in StringBuilder itself at makeURL(), i replaced it with String then everything worked without any exceptions. makeURL() after editing:
private String makeUrl(String textFromEditText) { String oldLink = textFromEditText; textFromEditText="https://www.googleapis.com/books/v1/volumes?download=DOWNLOAD_UNDEFINED&filter=FILTER_UNDEFINED&libraryRestrict=LIBRARY_RESTRICT_UNDEFINED&maxResults=20&orderBy=relevance&printType=ALL&q="; textFromEditText+=URLEncoder.encode(oldLink); textFromEditText+="&key=AIzaSyCJmeSLPw147mRSnc2nZNgPSbebtH-RxDc"; Log.v("makeURL",stringBuilder.toString()); return textFromEditText; }