【发布时间】:2011-12-14 01:04:53
【问题描述】:
我的 HttpClient、HttpPost 或 HttpGet 方法只工作一次。我第一次可以从响应中成功登录获取 html,但是当我第二次尝试登录时,html 是空白的,我什至没有得到响应。怎么了?
public void parseDoc() {
new Thread(new Runnable() {
@Override
public void run() {
sting.loadData("<p></p>", "text/html", null);
HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, true);
httpclient = new DefaultHttpClient();
httppost = new HttpPost(
"https://secure.groupfusion.net/processlogin.php");
String HTML = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
3);
nameValuePairs
.add(new BasicNameValuePair(
"referral_page",
"/modules/gradebook/ui/gradebook.phtml?type=student_view&jli=t&jli=t&jli=t&jli=t&jli=t&jli=t&printable=FALSE&portrait_or_landscape=portrait"));
nameValuePairs.add(new BasicNameValuePair("currDomain",
"beardenhs.knoxschools.org"));
nameValuePairs
.add(new BasicNameValuePair("username", user));
nameValuePairs
.add(new BasicNameValuePair("password", pass));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HTML = EntityUtils.toString(response.getEntity());
Document doc = Jsoup.parse(HTML);
Element link = doc.select("a").first();
linkHref = link.attr("href");
HttpGet request = new HttpGet();
try {
request.setURI(new URI(linkHref));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response = httpclient.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line);
}
in.close();
HTML = str.toString();
doc = Jsoup.parse(HTML);
Elements divs = doc.getElementsByTag("tbody");
for (Element d : divs) {
if (i == 2) {
finishGrades();
f++;
break;
}
i++;
ggg = d.html();
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}).start();
}
【问题讨论】:
-
您是否尝试从该线程访问 UI 元素?
finishGrades()是做什么的? -
这是完成成绩的代码:
public void finishGrades() { htmll = "<html><head><title>Grades</title></head><body><table><tbody>\n" + ggg + "</tbody></table></body></html>"; sting.loadData(htmll, "text/html", null); dialog.dismiss(); f++; },因为它将 html 加载到 Web 视图中。问题是,我第二次登录时没有杀死应用程序,我没有得到响应。这意味着没有 html。这可能是什么原因造成的? -
我会尝试将响应代码打印到 Logcat,这可能会提供有关响应状态的一些线索。
标签: java android httpclient http-post http-get