Hi, I’m Sagnik Ganguly! I’m creating an Android application which is a music player and I’m trying to fetch the images of the artists from MusicBrainz but to get the the images I’ve to find the MBID of the artist first and obviously I can’t manually enter the MBID of all the individual artists so I’m trying to retrieve the MBID from https://musicbrainz.org/ws/2/artist?query=Ellie%20Goulding and it’s perfectly working on the browser but in my application I’m getting a HTTP 400 Bad Request error. May I know what is the cause of it?
Here is my Logcat output:
E/XMLPARSER:URL: https://musicbrainz.org/ws/2/artist?query=Arijit%20Singh
E/XMLPARSER:REQUEST: {Accept=[text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/webp,*/*;q=0.8], Accept-Charset=[ISO-8859-1,utf-8;q=0.7,*;q=0.7], Accept-Encoding=[gzip, deflate, br], Accept-Language=[en-us,en;q=0.5], Connection=[keep-alive], Content-type=[application/xml], Host=[musicbrainz.org], Keep-Alive=[115], User-Agent=[Flow/1.0.1 (sagnikganguly2012@rediffmail.com)]}
E/XMLPARSER:RESPONSE: {null=[HTTP/1.1 400 Bad Request], Access-Control-Allow-Origin=[*], Connection=[keep-alive], Content-Length=[167], Content-Type=[application/json; charset=utf-8], Date=[Sat, 30 Jan 2021 13:36:27 GMT], ETag=["12537101369f5abd47cc74cb0dd23771"], Keep-Alive=[timeout=15], Server=[Plack::Handler::Starlet], X-Android-Received-Millis=[1612013787903], X-Android-Response-Source=[NETWORK 400], X-Android-Selected-Protocol=[http/1.1], X-Android-Sent-Millis=[1612013787070], X-RateLimit-Limit=[1200], X-RateLimit-Remaining=[771], X-RateLimit-Reset=[1612013787]}
D/XMLPARSER:: http response code is 400 : Bad Request
Here is my code:
private String request(String urlString) {
final StringBuilder sb = new StringBuilder();
try{
URL url = new URL(urlString);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
urlString = uri.toASCIIString();
url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Host", "musicbrainz.org");
connection.setRequestProperty("User-Agent", "Flow/1.0.1 (sagnikganguly2012@rediffmail.com)");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/webp,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Keep-Alive", "115");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
connection.setRequestProperty("Content-type", "application/xml");
connection.setDoInput(true);
connection.setDoOutput(true);
Log.e("XMLPARSER:URL", urlString);//request headers)
Log.e("XMLPARSER:REQUEST", connection.getRequestProperties().toString());//request headers)
//Log.e("XMLPARSER:REQUEST", connection.get().toString());//request headers)
connection.connect();
Log.e("XMLPARSER:RESPONSE", connection.getHeaderFields().toString()); //response headers
//Log.e("XMLPARSER:", conn.getResponseCode());//response http code
InputStream inputStream;
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.d("XMLPARSER:", "http response code is " + connection.getResponseCode()+" : "+connection.getResponseMessage());
//return null;
inputStream = new GZIPInputStream(connection.getErrorStream());
} else
inputStream = new GZIPInputStream(connection.getInputStream());
//InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
sb.append(line).append("\n");
}
Log.d("XMLPARSER:", "RESPONSE: " + sb.toString());
}
catch (IOException | URISyntaxException e) {
// Writing exception to log
e.printStackTrace();
}
return sb.toString();
}