gpt4 book ai didi

Android 登录页面,HTTPPOST(可能是 AsyncTask 帮助)

转载 作者:行者123 更新时间:2023-11-30 04:00:39 25 4
gpt4 key购买 nike

我正在尝试创建一个登录页面以从我学校的网站上提取一些信息(日程信息等)。该代码作为 java 驱动程序完美运行,但我正在尝试让它在 android 上运行。一位 friend 告诉我应该使用 AsyncTask 模式。有人可以用下面的代码告诉我如何使用 AsyncTask 吗?

public class MainActivity extends Activity implements OnClickListener {
EditText un,pw;
TextView error;
Button ok;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
un=(EditText)findViewById(R.id.et_un);
pw=(EditText)findViewById(R.id.et_pw);

ok=(Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
}

public void postLoginData() {
//Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost=new HttpPost("url");

try {
EditText un=(EditText)findViewById(R.id.et_un);
String username=un.getText().toString();

EditText pw=(EditText)findViewById(R.id.et_pw);
String password=un.getText().toString();

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("userName", new StringBody("username"));
entity.addPart("password", new StringBody("password"));
entity.addPart("btnLogin", new StringBody("Login"));
entity.addPart("__EVENTTARGET", new StringBody(""));

//Post to login site
httppost = new HttpPost("https://my.jcsu.edu/ICS");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity response_entity = response.getEntity();
if (response_entity != null) {
Jsoup.parse(EntityUtils.toString(response_entity));
}

HttpGet httpget = new HttpGet("url");
response = httpclient.execute(httpget);
response_entity = response.getEntity();


if (response_entity != null) {
Document doc = Jsoup.parse(EntityUtils.toString(response_entity));

// Get the user's name
Element userWelcome = doc.getElementById("userWelcome");
System.out.println("Welcome " + userWelcome.getElementsByTag("strong").get(0).html());

// Get the user's schedule
System.out.println("\nCourse Schedule:");
Elements gbody = doc.getElementsByClass("gbody");
Element tr = gbody.get(4);
Elements td = tr.getElementsByTag("td");
for (Element e : td) {
if (e.html().contains("<ul>") || e.html().contains("<a"))
continue;
else

System.out.println(e.html());
}

// Get user's information
System.out.println("\nAcademic Information:");
System.out.println(doc.getElementById("pg7_V_rptPackage_ctl00_lblDivision").html()
.replace("&nbsp;", ""));
System.out.println("Faculty Advisors: "
+ doc.getElementById("pg7_V_rptPackage_ctl00_rptAdvisor_ctl00_lblAdvisorInfo").html() + ", "
+ doc.getElementById("pg7_V_rptPackage_ctl00_rptAdvisor_ctl02_lblAdvisorInfo").html());
System.out.println("Intended Majors: "
+ doc.getElementById("pg7_V_rptPackage_ctl00_rptMajor_ctl00_lblMajorInfo").html());

}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void onClick(View view) {
if(view == ok){
postLoginData();
}
}

}

最佳答案

OnClickListener.onClick 方法将从 UI 线程调用,因此您不应从该方法执行任何长时间运行的操作。在 onClick 中,您调用 postLoginData 来发出网络请求。您不应该从 UI 线程执行此操作。

除非我遗漏了什么,否则我实际上看不出有任何理由使用上面的 AsyncTask,您可以简单地使用一个新的 Thread 来完成后台工作。这是因为您的方法 postLoginData 只是发出请求并将一些内容打印到 System.out

public void postLoginData() {
new Thread() {
// ... body of postLoginData()
}.start();
}

但是,我假设您实际上还有一些不属于您的代码段的代码,或者稍后会添加一些。如果该代码将在网络操作完成后更新 UI,您应该考虑使用 AsyncTask。我将尝试勾勒出一个使用 one 的示例。

您需要从 UI 线程对 UI 进行任何更新,但您不能在 UI 线程上执行长时间运行的操作,例如网络请求。 Android AsyncTask 背后的基本思想是在 UI 线程和后台线程上安排工作。这允许您开始下载数据,在下载发生时更新进度条,并在下载完成时更新 UI。下面是如何使用 AsyncTask 实现 postLoginData 的基本草图:

PostLoginDataTask extends AsyncTask {
String username, password;

onPreExecute() { // runs on the UI thread
EditText un=(EditText)findViewById(R.id.et_un);
username=un.getText().toString();

EditText pw=(EditText)findViewById(R.id.et_pw);
password=un.getText().toString();
}

doInBackground() { // runs on a background thread
//Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost=new HttpPost("url");

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("userName", new StringBody("username"));
entity.addPart("password", new StringBody("password"));
entity.addPart("btnLogin", new StringBody("Login"));
entity.addPart("__EVENTTARGET", new StringBody(""));

//Post to login site
httppost = new HttpPost("https://my.jcsu.edu/ICS");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity response_entity = response.getEntity();
if (response_entity != null) {
Jsoup.parse(EntityUtils.toString(response_entity));
}

HttpGet httpget = new HttpGet("url");
response = httpclient.execute(httpget);
response_entity = response.getEntity();

return response_entity;
}

onPostExecute() { // runs on the UI thread
if (response_entity != null) {
Document doc = Jsoup.parse(EntityUtils.toString(response_entity));

// Get the user's name
Element userWelcome = doc.getElementById("userWelcome");
System.out.println("Welcome " + userWelcome.getElementsByTag("strong").get(0).html());

// Get the user's schedule
System.out.println("\nCourse Schedule:");
Elements gbody = doc.getElementsByClass("gbody");
Element tr = gbody.get(4);
Elements td = tr.getElementsByTag("td");
for (Element e : td) {
if (e.html().contains("<ul>") || e.html().contains("<a"))
continue;
else

System.out.println(e.html());
}

// Get user's information
System.out.println("\nAcademic Information:");
System.out.println(doc.getElementById("pg7_V_rptPackage_ctl00_lblDivision").html()
.replace("&nbsp;", ""));
System.out.println("Faculty Advisors: "
+ doc.getElementById("pg7_V_rptPackage_ctl00_rptAdvisor_ctl00_lblAdvisorInfo").html() + ", "
+ doc.getElementById("pg7_V_rptPackage_ctl00_rptAdvisor_ctl02_lblAdvisorInfo").html());
System.out.println("Intended Majors: "
+ doc.getElementById("pg7_V_rptPackage_ctl00_rptMajor_ctl00_lblMajorInfo").html());

// Any updates to the UI should go in this method
}
}
}

请注意,这并不完全正确,不会编译。你需要 read up on the generic type parameters Params, Progress and Result,并弄清楚如何将其融入您的代码。我把它留给读者作为练习 :P 这并不太难,但如果您感到困惑,请随时在评论中提问...

希望对您有所帮助!

关于Android 登录页面,HTTPPOST(可能是 AsyncTask 帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12550039/

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