gpt4 book ai didi

具有特定端口的 Android HTTP 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:50 32 4
gpt4 key购买 nike

我正在尝试从 Shoutcast 服务器获取歌曲名称。所以,我的想法是在 Shoutcast 服务器的 7.html 页面上做一些正则表达式,但我无法获得简单的 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): 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)

最佳答案

我想通了!我的 String url 变量实际上有问题。在使用 URLEncode 对该字符串进行编码后,我看到它的开头有 %0A%0D,因为我是从文件中获取这些 url 的。 (%0A%0D 是用于转义和换行的字符,请参阅 here 更多信息)现在一切都像魅力一样运作!伙计们,在发出 Http 请求之前,请务必仔细检查您的网址。

关于具有特定端口的 Android HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14593329/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com