Android登录页面,HTTPPOST(可能的异步任务帮助)
我试图创建一个登录页面,从我学校的网站上获取一些信息(日程安排信息等)。这段代码作为java驱动程序运行得非常完美,但我正在尝试让它在android上运行。一位朋友告诉我应该使用Android登录页面,HTTPPOST(可能的异步任务帮助),android,login,android-asynctask,http-post,Android,Login,Android Asynctask,Http Post,我试图创建一个登录页面,从我学校的网站上获取一些信息(日程安排信息等)。这段代码作为java驱动程序运行得非常完美,但我正在尝试让它在android上运行。一位朋友告诉我应该使用AsyncTask模式。有人能告诉我如何将AsyncTask与下面的代码一起使用吗 public class MainActivity extends Activity implements OnClickListener { EditText un,pw; TextView error; But
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(" ", ""));
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();
}
}
}
public类MainActivity扩展活动实现OnClickListener{ 编辑文本un,pw; 文本视图错误; 按钮ok; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); un=(编辑文本)findViewById(R.id.et_un); pw=(编辑文本)findViewById(R.id.et_pw); ok=(按钮)findViewById(R.id.btn\u登录); 好的,setOnClickListener(这个); } public void postloginanda(){ //创建一个新的HttpClient和Post头 DefaultHttpClient httpclient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(“url”); 试一试{ EditText un=(EditText)findViewById(R.id.et_un); 字符串username=un.getText().toString(); EditText pw=(EditText)findViewById(R.id.et_pw); 字符串密码=un.getText().toString(); MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容); 实体=新的多个实体(HttpMultipartMode.BROWSER_兼容); entity.addPart(“用户名”),新的StringBody(“用户名”); entity.addPart(“密码”),新的StringBody(“密码”); entity.addPart(“btnLogin”,新的StringBody(“登录”); entity.addPart(“\uuuEventTarget”,新的StringBody(”); //发布到登录站点 httppost=新的httppost(“https://my.jcsu.edu/ICS"); httppost.setEntity(实体); HttpResponse response=httpclient.execute(httppost); HttpEntity response_entity=response.getEntity(); if(响应实体!=null){ parse(EntityUtils.toString(response_entity)); } HttpGet HttpGet=新的HttpGet(“url”); response=httpclient.execute(httpget); response_entity=response.getEntity(); if(响应实体!=null){ Document doc=Jsoup.parse(EntityUtils.toString(response_entity)); //获取用户名 元素userWelcome=doc.getElementById(“userWelcome”); System.out.println(“Welcome”+userWelcome.getElementsByTag(“strong”).get(0.html()); //获取用户的时间表 System.out.println(“\n课程计划:”); 元素gbody=doc.getElementsByClass(“gbody”); 元素tr=gbody.get(4); 元素td=tr.getElementsByTag(“td”); 对于(要素e:td){
。我将尝试用一个例子来画出一个例子。 您需要从UI线程对UI进行任何更新,但不能在UI线程上执行长时间运行的操作,如网络请求。Android的
如果(e.html()包含(“”)e.html()包含(“方法
将从UI线程调用,因此您不应该从该方法执行任何长时间运行的操作。从OnClickListener.onClick
内部调用发出网络请求的onClick
。您不应该从UI线程执行该操作 除非我遗漏了什么,否则我实际上看不出有任何理由使用上面的postloginada
,你可以简单地使用一个新的异步任务
来做后台工作。这是因为你的方法线程
只是发出一个请求并将一些内容打印到postloginanda
System.out
public void postLoginData() { new Thread() { // ... body of postLoginData() }.start(); }
但是,我假设你有更多的代码不是你的代码片段的一部分,或者稍后会添加一些代码。如果代码在网络操作完成后将更新UI,你应该考虑使用<代码> AycCastase
背后的基本思想是安排UI线程和后台线程的工作。这允许您开始下载数据,在下载时更新进度条oad发生,并在完成时更新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(" ", "")); 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 } } }
PostLoginDatask扩展异步任务{ 字符串用户名、密码; onPreExecute(){//在UI线程上运行 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(){//在后台线程上运行 //创建一个新的HttpClient和Post头 DefaultHttpClient httpclient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(“url”); MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容); 实体=新的多个实体(HttpMultipartMode.BROWSER_兼容); entity.addPart(“用户名”),新的StringBody(“用户名”); entity.addPart(“密码”),新的StringBody(“密码”); entity.addPart(“btnLogin”,新的StringBody(“登录”); entity.addPart(“\uuuEventTarget”,新的StringBody(”); //发布到登录站点 httppost=新的httppost(“https://my.jcsu.edu/ICS"); httppost.setEntity(实体); HttpResponse response=httpclient.execute(httppost); HttpEntity response_entity=response.getEntity(); if(响应实体!=null){ parse(EntityUtils.toString(response_entity)); } HttpGet HttpGet=新的HttpGet(“url”); response=httpclient.execute(httpget); response_entity=response.getEntity(); 返回响应实体; } onPostExecute(){//在UI上运行