我正在尝试将此链接的正文存储在 String HTML 中,然后将其加载到 WebView。(解析代码是用 Jsoup 完成的)。当我按下按钮时,调试器显示String HTML = re.fixCode(); 并说“调试当前指令指针”。
我尝试了很多事情,比如在方法中传递字符串并将字符串作为它传递。但同样的事情一遍又一遍地发生。我的结论是它没有将方法/变量传递给我的其他类?(希望你明白最后一点)
如果是这种情况,我该如何访问它们?通过意图?
所以要清楚,我想获取正文,将其传递给字符串,然后将其加载到 WebView 中。谢谢!
主班
public class MainActivity extends Activity {
private String uri = "http://www.novasoftware.se/webviewer/%28S%28muz0tu55twfd43zkqrlejb55%29%29/design1.aspx?schoolid=18200&code=83310";
private RequestTask re;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
final WebView web = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new RequestTask().execute(uri);
loadWeb();
}
});
}
private void loadWeb() {
**String HTML = re.fixCode();**
System.out.println(HTML);
// web.loadData(re.fixedHtml, "text/html", null);
}
}
请求任务类
package com.dir.schema;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.AsyncTask;
public class RequestTask extends AsyncTask<String, String, String>{
public String html, fixedHtml;
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
html = result;
fixCode();
}
public String fixCode() {
Document doc = Jsoup.parse(html);
return fixedHtml = doc.body().toString();
}
}