gpt4 book ai didi

java - 无法使用http请求获取页面

转载 作者:行者123 更新时间:2023-12-02 07:19:27 25 4
gpt4 key购买 nike

这是代码,它必须加载页面并将其存储在string[]中。我不明白这个问题,因为它没有加载页面并且没有抛出任何错误

如果有人知道任何发送 http 请求的更好方法,请发布或者请告诉我问题。

 //some code    
btnShowLocation.setOnClickListener(new View.OnClickListener() {
int j=0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

new MyTask().execute();});
//some code

private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub

try {

String ssr="";
URL n = new URL("http://maps.google.co.in/maps?hl=en&q=nagpur+to+pune");
URLConnection nc = null;
nc = n.openConnection();
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(nc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
ssr+=inputLine;
}

Document doc = Jsoup.parse(ssr);
Elements el = doc.getElementsByClass("dir-mrgnr");
String str = el.text();
str = str.replaceAll("[0-9]+[.] ", "\n");
string = str.split("\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
Toast.makeText(
getApplicationContext(),
"in Background",
Toast.LENGTH_LONG).show();
speakOut("not Working");
//speakOut("work in progress");
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub

super.onPostExecute(result);

}

}

最佳答案

您不需要下载页面并解析它。

List<String> list = new ArrayList<>(); // Better than an array

Document doc = Jsoup.connect("http://maps.google.co.in/maps?hl=en&q=nagpur+to+pune").get(); // Connect to url and parse its conntent
Elements el = doc.select("*.dir-mrgnr"); // Every tag with 'dir-mrgnr' class - or use getElementsByClass() as you did

for( Element element : el )
{
list.add(element.text());
}

顺便说一句。 不要使用+= on a String in a loop ;使用 StringBuilder instdead!

关于java - 无法使用http请求获取页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14437581/

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