我创建了一个应用程序,用于在登录该网站后使用jsoup从网站获取表到android。虽然它运行良好但速度非常慢,并且需要花费大量时间来启动活动。任何人都可以帮我解决这个问题。我已附上以下代码,并提前致谢
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tv=(TextView)findViewById(R.id.tv);
Bundle extra=getIntent().getExtras();
if(extra!=null){
Log.v("got", extra.getString("user")+extra.getString("pass"));
user=extra.getString("user");
pass=extra.getString("pass");
}
HomeList homelist=new HomeList(this);
try {
ArrayList<String> result=homelist.execute(user+pass).get();
if(result.contains("rejected")){
Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show();
finish();
}
else{
tv.setText(user.toUpperCase(Locale.US));
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result);
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
public class HomeList extends AsyncTask<String,String,ArrayList<String>> {
private List<String> cookies;
private HttpURLConnection conn;
private final String USER_AGENT="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36";
private Home home;
List<String> cook;
private int BUFF=50;
String username;
public HomeList(Home home) {
// TODO Auto-generated constructor stub
this.home=home;
}
public HomeList() {
// TODO Auto-generated constructor stub
}
@Override
protected ArrayList<String> doInBackground(String... arg0) {
// TODO Auto-generated method stub
publishProgress("Loading");
String user=arg0[0].substring(0,10);
Log.v("user", user);
String pass=arg0[0].substring(10,arg0[0].length());
Log.v("pass", pass);
//int code=Integer.parseInt(arg0[0].substring(arg0[0].length()-1));
ArrayList<String> no=new ArrayList<String>();
no.add("rejected");
String url = "http://borealis.astra.edu.in";
//action url in form
String astra="http://borealis.astra.edu.in/index.php";
String attendance="http://borealis.astra.edu.in/index.php?option=com_base_attendancereport&Itemid=98";
HomeList coll=new HomeList();
//for cookies
CookieHandler.setDefault(new CookieManager());
//get form data to be sent
String page=coll.GetPageContent(url);
//collecting form data
String postParams = coll.getFormParams(page,user,pass);
System.out.println(postParams);
cook=cookies;
System.out.print(cook);
ArrayList<String> result=coll.sendPost(url,postParams);
if(result.contains("error"))
return no;
else
return result;
}
private ArrayList<String> sendPost(String url, String postParams) {
// TODO Auto-generated method stub
URL obj;
try {
obj = new URL(url);
conn=(HttpURLConnection)obj.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "borealis.astra.edu.in");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language","en-US,en;q=0.8");
for(String cookie:this.cookies){
conn.setRequestProperty("cookie", cookie.split(";",1)[0]);
System.out.println(cookie);
}
conn.setRequestProperty("Connection","keep-alive");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream wr=new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode=conn.getResponseCode();
System.out.println(conn.getURL());
System.out.println("posting data to "+url);
System.out.println("parametrs are "+postParams);
System.out.println("response Code "+responseCode);
URL secondURL = new URL(conn.getHeaderField("Location"));
conn=(HttpURLConnection) secondURL.openConnection();
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()),BUFF);
Log.d("length", "got"+conn.getContentLength());
System.out.println("redirected url" + conn.getURL());
String inputLine;
StringBuffer response=new StringBuffer();
while((inputLine=in.readLine())!=null){
response.append(inputLine);
}
Document doc=Jsoup.parse(response.toString());
Elements table=doc.getElementsByClass("tiles_box");
in.close();
ArrayList<String> ele=new ArrayList<String>();
if(table.first()!=null){
Iterator<Element> ite=table.select("tr").select("td").iterator();
ele.add(ite.next().text());
Log.d("td", ele.toString());
while(ite.hasNext()){
String td=ite.next().text();
if(!ele.contains(td)){
ele.add(td);
Log.d("td", ele.toString());
}
}
return ele;
}
else
{
ArrayList<String> error=new ArrayList<String>();
error.add("error");
return error;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private String getFormParams(String html, String user, String pass){
// TODO Auto-generated method stub
try{
System.out.println("extracting form data");
Document doc=Jsoup.parse(html);
Element form=doc.getElementById("form-login");
Elements inputElements=form.getElementsByTag("input");
List<String> paramsList=new ArrayList<String>();
for(Element inputElement:inputElements){
String key=inputElement.attr("name");
String value=inputElement.attr("value");
if(key.equals("username"))
value=user;
if(key.equals("passwd"))
value=pass;
paramsList.add(key+"="+URLEncoder.encode(value,"UTF-8"));
}
StringBuilder result=new StringBuilder();
for(String param:paramsList){
if(result.length()==0){
result.append(param);
}else{
result.append("&"+param);
}
}
return result.toString();
}catch(Exception e){
Log.d("error", e.toString());
}
return null;
}
private String GetPageContent(String url){
// TODO Auto-generated method stub
try{
URL obj=new URL(url);
conn=(HttpURLConnection)obj.openConnection();
conn.setChunkedStreamingMode(20);
conn.setRequestMethod("GET");
conn.setUseCaches(true);
conn.setRequestProperty("Host", "borealis.astra.edu.in");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language","en-US,en;q=0.8");
if(cookies!=null)
{
for(String cookie:this.cookies){
conn.addRequestProperty("cookie", cookie.split(";",1)[0]);
}
}
int responseCode=conn.getResponseCode();
System.out.println("sending get request "+url);
System.out.println("response code is "+responseCode);
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()),BUFF);
String inputLine;
StringBuffer response=new StringBuffer();
while((inputLine=in.readLine())!=null){
response.append(inputLine);
}
in.close();
setCookies(conn.getHeaderFields().get("Set-Cookie"));
return response.toString();
}catch(Exception e){
Log.d("error", e.toString());
}
return null;
}
private void setCookies(List<String> cookies) {
// TODO Auto-generated method stub
this.cookies=cookies;
cook=cookies;
System.out.print(cook);
}
public List<String> getCookies(){
return cookies;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
Toast.makeText(getApplicationContext(), values[0], Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
答案 0 :(得分:0)
使用此行ArrayList<String> result=homelist.execute(user+pass).get();
,您将失去多线程的所有优势。问题是get()
方法,
如果需要等待计算完成,然后检索其结果。
在我看来,你应该使用一种回调,如:
public class MyAsyncTask extends AsyncTask<String, Void, String> {
public interface OnWorkDone {
public void doSomething(String s);
}
private OnWorkDone listener;
public void setListener(OnWorkDone listener) {
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
String result = null;
//do something
return result;
}
@Override
protected void onPostExecute(String result) {
if (listener != null) {
listener.doSomething(result);
}
}
}
而且,在主要班级:
MyAsyncTask task = new MyAsyncTask();
task.setListener(new OnWorkDone() {
@Override
public void doSomething(String s) {
//Use results
}
});
task.execute(params);