我目前的问题是我正在尝试.post()
使用慢速服务器的用户名和密码。当我绕过在主线程上运行我的网络代码时,我的方法工作正常。我决定停止偷懒并将我的代码移动到 AsyncTask 线程,但是 JSOUP 开始忽略.timeout()
我设置的时间,这导致了 408 错误。
这是我的代码。用户名和密码被审查。网址也被审查。Logcat 显示代码工作正常。
package com.example.androidgenparsetest;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Map;
import android.os.AsyncTask;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GetGenesis().execute("ca.com","ca3");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class GetGenesis extends AsyncTask<String, Void, Document>
{
protected Document doInBackground(String... UserPass)
{
Document doc2 = null;
try
{
Connection.Response res = Jsoup
.connect("http://j_security_check")
.data("j_username",UserPass[0],"j_password",UserPass[1])
.method(Method.POST)
.userAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5")
.timeout(2*1000)
.ignoreHttpErrors(true)
.execute();
Map<String, String> cookies = res.cookies();
doc2 = Jsoup
.connect("/j_security_check")
.data("j_username",UserPass[0],"j_password",UserPass[1])
.method(Method.POST)
.userAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5")
.timeout(2*1000)
.ignoreHttpErrors(true)
.cookies(cookies)
.get();
}
catch (IOException e)
{
e.printStackTrace();
}
return doc2;
}
protected void onPostExecute(Document doc)
{
Document doc2=doc;
TextView htmltext=(TextView)findViewById(R.id.htmltext);
htmltext.setText(doc2.text());
}
}
}