Search code examples
androidhttpshoutcasticecast

Android HTTP request with specific port


I am trying to get the song title from a Shoutcast server. So, my idea was to do a little regexp on 7.html page of Shoutcast server, BUT I can't get simple HttpGet request to receive 7.html page. What am I doing wrong?

If I remove port number from link, HttpGet will run without any problems, but I won't get my results.

private class GetTrackInfo extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... Urls) {

        String url = urls[0];
        if(!url.contains("http://")) url = "http://" + url;
        url = url + "/7.html";
        HttpParams params = new BasicHttpParams();
        HttpClient httpclient = new DefaultHttpClient(params);
        HttpGet http = new HttpGet(url);
        HttpResponse response = null;
        try {
            response = httpclient.execute(http);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            response.getEntity().writeTo(out);
        } catch (IOException e) {
            e.printStackTrace();
        }        
        String res = out.toString();
        return res;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i("GetTrack", "Track result: " + result);
        String[] results = result.split(",");
        String track = results[results.length-1];
        fplayer.setStreamInfoTxt(track);
    }

}

As an error, I get:

01-29 23:28:10.461: W/System.err(962): org.Apache.http.client.ClientProtocolException 01-29 23:28:10.471: W/System.err(962): at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:557) 01-29 23:28:10.471: W/System.err(962): at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:487) 01-29 23:28:10.471: W/System.err(962): at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:465) 01-29 23:28:10.471: W/System.err(962): at com.imsgroups.exyuradio.services.PlayerService$GetTrackInfo.doInBackground(PlayerService.Java:257) 01-29 23:28:10.471: W/System.err(962): at com.imsgroups.exyuradio.services.PlayerService$GetTrackInfo.doInBackground(PlayerService.Java:1) 01-29 23:28:10.481: W/System.err(962): at Android.os.AsyncTask$2.call(AsyncTask.Java:185) 01-29 23:28:10.481: W/System.err(962): at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:305) 01-29 23:28:10.491: W/System.err(962): at Java.util.concurrent.FutureTask.run(FutureTask.Java:137) 01-29 23:28:10.491: W/System.err(962): at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1068) 01-29 23:28:10.491: W/System.err(962): at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:561) 01-29 23:28:10.491: W/System.err(962): at Java.lang.Thread.run(Thread.Java:1096) 01-29 23:28:10.491: W/System.err(962): Caused by: org.Apache.http.ProtocolException: The server failed to respond with a valid HTTP response 01-29 23:28:10.511: W/System.err(962): at org.Apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.Java:93) 01-29 23:28:10.511: W/System.err(962): at org.Apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.Java:174) 01-29 23:28:10.511: W/System.err(962): at org.Apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.Java:179) 01-29 23:28:10.511: W/System.err(962): at org.Apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.Java:235) 01-29 23:28:10.522: W/System.err(962): at org.Apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.Java:259) 01-29 23:28:10.522: W/System.err(962): at org.Apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.Java:279) 01-29 23:28:10.522: W/System.err(962): at org.Apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.Java:121) 01-29 23:28:10.522: W/System.err(962): at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:410) 01-29 23:28:10.541: W/System.err(962): at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:555)


Solution

  • I managed to figure it out! I actually had problem with my String url variable. After encoding this String with URLEncode I saw that I have %0A%0D at the of it since I got those urls from a file. (%0A%0D are chars for escape and new line, see here more about it) Everything is working like charm now! So guys, always double check your url before you make Http request.