学校のウェブサイトから情報を取得するためのログイン ページを作成しようとしています (スケジュール情報など)。コードはJavaドライバーとして完全に機能しますが、Androidで動作するようにしようとしています。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();
}
}
}