我正试图从Shoutcast服务器获取歌曲名称。所以,我的想法是在Shoutcast服务器的7.html页面做一点regexp,但是我无法得到简单的HttpGet请求来接收7.html页面。我做错了什么?
如果我从链接中删除端口号,HttpGet将运行没有任何问题,但我不会得到我的结果。
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);
}
}
作为一个错误,我得到:
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):在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):引起:org.Apache.http.ProtocolException:服务器无法响应有效的HTTP响应 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)
答案 0 :(得分:1)
我设法搞清楚了!我实际上遇到了String url
变量的问题。
用URLEncode对这个字符串进行编码后,我发现我有%0A%0D,因为我从文件中获取了这些url。 (%0A%0D是转义和换行的字符,有关它的更多信息,请参阅here)
现在一切都像魅力一样!所以大家,在你发出Http请求之前,总是仔细检查你的网址。
答案 1 :(得分:0)
您正在修改变量url
,但随后将原始参数传递给HttpGet
HttpGet http = new HttpGet(urls[0]);
因此,如果您的urls[0]
已经没有http:
协议,那么它就不会被添加。我想你想要以下内容 -
HttpGet http = new HttpGet(url);