我必须使用java类,一个是活动,另一个是我想从中运行void的类。 Web.java看起来像:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Web {
public Document requestPage(String urll) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urll);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
Document doc = Jsoup.parse(result.toString());
return doc;
}
}
我的MainActivity.java看起来像这样:
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class MainActivity extends AppCompatActivity {
Button Visit;
EditText Urlbox;
Web web = new Web();
@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);
Visit = (Button)findViewById(R.id.Button);
Urlbox = (EditText)findViewById(R.id.EditText);
Visit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Document lol = web.requestPage(Urlbox.getText().toString());
if(lol.text().toString() != null){
Toast.makeText(MainActivity.this, "Visit success!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
这对主线程最好,但我想让它通过一个不同的线程,所以如果连接不好,我的UI不会卡住 我如何在异步中完成这项工作?这是我第一次使用异步,但我只是不明白我应该如何使用它,我花了几天试图找出它绝对没有成功 所以,如果我能得到一些做得很好的例子,那么我可以将它与我当前的代码进行比较并了解我做错了什么
答案 0 :(得分:0)
我使用callback格式处理情境是Asynchronous networking
的最佳选择,或者你可以使用AsyncTask
但我解释cllback
模式让我们让它成为现实
首先创建callback
界面
public interface Request {
void onSuccess(Document doc);
}
2 - 实现Runnable
以在另一个线程上运行Asynchronous
请求
public class Web implements Runnable {
public final Request request;
private final String urll;
Web(String urll, Request request){
this.urll = urll;
this.request = request;
}
@Override public void run() {
try{
StringBuilder result = new StringBuilder();
URL url = new URL(urll);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
Document doc = Jsoup.parse(result.toString());
// fill the cllback with result
this.request.onSuccess(doc);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// return doc;
}
}
3 - 现在我们使用ExecutorService
(ThreadPools
因为理由而创建了它的主题),简单的事情就在这里我们使用new Thread(Runnable)
之前允许implement
calback
public class MainActivity extends AppCompatActivity implement Request{
Button Visit;
EditText Urlbox;
private Document lol;
//Web web = new Web();
@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);
Visit = (Button)findViewById(R.id.Button);
Urlbox = (EditText)findViewById(R.id.EditText);
Visit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Document lol = web.requestPage(Urlbox.getText().toString());
String url = Urlbox.getText().toString();
Thread thread = new Thread(new Web(url, this));
thread.start();
if(lol.text().toString() != null){
Toast.makeText(MainActivity.this, "Visit success!",Toast.LENGTH_SHORT).show();
}
thread.interrupt();
}
});
}
@Override
public void onSuccess(Document doc) {
lol = doc;
}
}
并且您需要的每个地方new
都会产生或请求您必须创建线程,如果您想分享结果,请在类或线程中分享这是另一回事
这种模式非常适合我。