当我使用以下代码向eclipse中的给定网站发出get请求时:
public String getLink(){
vidLink = "https://www.youtube.com/results?search_query=paradise+city+guns+n+roses+audio+only";
try {
URL url = new URL(vidLink);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
Scanner s = new Scanner(in).useDelimiter("\\A");
result = s.hasNext() ? s.next() : "";
}
catch(Exception e){
System.out.println("error");
我得到了回应(存储在可变结果中)
<li><div class="yt-lockup yt-lockup-tile yt-lockup-video vve-check clearfix" data-context-item-id="CutFtFcZLrA"
(这是我想要的)
然而,当我使用异步任务在android studio中发出相同的get请求时,使用以下代码:
public class HttpGetRequest extends AsyncTask<String, Void, String> {
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;
String result;
String inputLine;
String sname = "hello";
@Override
protected String doInBackground(String... params){
try {
String stringUrl = "https://www.youtube.com/results?search_query=paradise+city+guns+n+roses+audio+only";
URL myUrl = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)
myUrl.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.connect();
InputStream in = new BufferedInputStream(connection.getInputStream());
Scanner s = new Scanner(in).useDelimiter("\\A");
result = s.hasNext() ? s.next() : "";
linkloc = result.indexOf(testString) + testString.length();
for(int i = linkloc; result.charAt(i) != ('\"'); i++){
sname += result.charAt(i) + "";
}
}
catch(IOException e){
e.printStackTrace();
sname = "Error";
}
return result;
}
我得到了响应(存储在可变结果中)的内容:&lt; !DOCTYPE html&gt; @ font-fac等....
两个请求之间有什么区别,如何修复android studio响应以获取eclipse中的响应?任何帮助将不胜感激!