如果我使用,写道该程序已停止工作。我不懂为什么。该程序旨在从示例页面获取文本,保存,然后在屏幕上读取和写入。我来给你一些日志。
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MainActivity extends ActionBarActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.i2);
View.OnClickListener l = new View.OnClickListener(){
public void onClick (View arg0) {
try {
Document string = (Document) Jsoup.connect("http://sportstream.cba.pl/login.txt").get(); //example website
Elements b = string.select("body");
PrintWriter zapis = new PrintWriter("test1.txt");
zapis.println(b);
zapis.close();
Scanner odczyt = new Scanner(new File("test1.txt"));
String text = odczyt.nextLine();
TextView asd = (TextView)findViewById(R.id.t1);
asd.setText(text);
} catch (IOException e) {
e.printStackTrace();
}
}};
b1.setOnClickListener(l);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
02-14 21:44:16.015 3282-3282 / com.example.bk.tetet E / AndroidRuntime: 致命异议:主要 android.os.NetworkOnMainThreadException 在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) at java.net.InetAddress.lookupHostByName(InetAddress.java:385) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) 在libcore.net.http.HttpConnection。(HttpConnection.java:70) 在libcore.net.http.HttpConnection。(HttpConnection.java:50) 在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) 在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 在libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 在libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 在libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) at org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:449) at org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:434) 在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181) 在org.jsoup.helper.HttpConnection.get(HttpConnection.java:170) at com.example.bk.tetet.MainActivity $ 1.onClick(MainActivity.java:35) 在android.view.View.performClick(View.java:4101) 在android.view.View $ PerformClick.run(View.java:17087) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4849) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 在dalvik.system.NativeStart.main(本地方法)