[英]App Crash when I put Online text in TextView
My App crashes when I run this code: 运行以下代码时,我的应用程序崩溃:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biografie);
setTitle("Biografie");
try {
// Create a URL for the desired page
URL url = new URL("http://xxx.nl/api/biografie.php?dataid=998");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
TextView text = (TextView)findViewById(R.id.biografieText);
text.setText(str);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
This is my Logcat output: 这是我的Logcat输出:
03-26 12:35:16.900: E/AndroidRuntime(30127): FATAL EXCEPTION: main 03-26 12:35:16.900: E/AndroidRuntime(30127): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.appone.artistone.reneevanginkel/nl.appone.artistone.reneevanginkel.Biografie}: android.os.NetworkOnMainThreadException 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.ActivityThread.access$600(ActivityThread.java:123) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.os.Handler.dispatchMessage(Handler.java:99) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.os.Looper.loop(Looper.java:137) 03-26 12:35:16.900: E/AndroidRuntime(3
03-26 12:35:16.900:E / AndroidRuntime(30127):致命异常:主03-26 12:35:16.900:E / AndroidRuntime(30127):java.lang.RuntimeException:无法启动活动ComponentInfo {nl。 appone.artistone.reneevanginkel / nl.appone.artistone.reneevanginkel.Biografie}:android.os.NetworkOnMainThreadException 03-26 12:35:16.900:E / AndroidRuntime(30127):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1956)03-26 12:35:16.900:E / AndroidRuntime(30127):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)03-26 12:35:16.900:E / AndroidRuntime(30127):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)03-26 12:35:16.900:E / AndroidRuntime(30127):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)03 -26 12:35:16.900:E / AndroidRuntime(30127):在android.os.Handler.dispatchMessage(Handler.java:99)03-26 12:35:16.900:E / AndroidRuntime(30127):在android.os .Looper.loop(Looper.java:137)03-26 12:35:16.900:E / AndroidRuntime(3 0127): at android.app.ActivityThread.main(ActivityThread.java:4429) 03-26 12:35:16.900: E/AndroidRuntime(30127): at java.lang.reflect.Method.invokeNative(Native Method) 03-26 12:35:16.900: E/AndroidRuntime(30127): at java.lang.reflect.Method.invoke(Method.java:511) 03-26 12:35:16.900: E/AndroidRuntime(30127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 03-26 12:35:16.900: E/AndroidRuntime(30127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 03-26 12:35:16.900: E/AndroidRuntime(30127): at dalvik.system.NativeStart.main(Native Method) 03-26 12:35:16.900: E/AndroidRuntime(30127): Caused by: android.os.NetworkOnMainThreadException 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 03-26 12:35:16.900: E/AndroidRuntime(30127): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 03-26 12:35:16.900: E/AndroidRuntime(30127): at java.net.InetAddress.getA
0127):位于android.app.ActivityThread.main(ActivityThread.java:4429)03-26 12:35:16.900:E / AndroidRuntime(30127):位于java.lang.reflect.Method.invokeNative(本机方法)03- 26 12:35:16.900:E / AndroidRuntime(30127):at java.lang.reflect.Method.invoke(Method.java:511)03-26 12:35:16.900:E / AndroidRuntime(30127):at com。 android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)03-26 12:35:16.900:E / AndroidRuntime(30127):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :562)03-26 12:35:16.900:E / AndroidRuntime(30127):在dalvik.system.NativeStart.main(本地方法)03-26 12:35:16.900:E / AndroidRuntime(30127):原因: android.os.NetworkOnMainThreadException 03-26 12:35:16.900:E / AndroidRuntime(30127):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)03-26 12:35:16.900:E / AndroidRuntime (30127):位于java.net.InetAddress.lookupHostByName(InetAddress.java:391)03-26 12:35:16.900:E / AndroidRuntime(30127):位于java.net.InetAddress.getA llByNameImpl(InetAddress.java:242) 03-26 12:35:16.900: E/AndroidRuntime(30127): at java.net.InetAddress.getAllByName(InetAddress.java:220) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpConnection.(HttpConnection.java:71) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpConnection.(HttpConnection.java:50) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:312) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpEngine.connect(HttpEngine.java:307) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpEngine.se
llByNameImpl(InetAddress.java:242)03-26 12:35:16.900:E / AndroidRuntime(30127):在java.net.InetAddress.getAllByName(InetAddress.java:220)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpConnection。(HttpConnection.java:71)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpConnection。(HttpConnection.java :50)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:351)03-26 12:35:16.900:E / AndroidRuntime (30127):位于libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpConnection.connect(HttpConnection。 java:128)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:312)03-26 12:35:16.900:E / AndroidRuntime( 30127):位于libcore.net.http.HttpEngine.connect(HttpEngine.java:307)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpEngine.se ndSocketRequest(HttpEngine.java:286) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:236) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 03-26 12:35:16.900: E/AndroidRuntime(30127): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 03-26 12:35:16.900: E/AndroidRuntime(30127): at java.net.URL.openStream(URL.java:462) 03-26 12:35:16.900: E/AndroidRuntime(30127): at nl.appone.artistone.reneevanginkel.Biografie.onCreate(Biografie.java:30) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.Activity.performCreate(Activity.java:4465) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 03-26 12:35:16.900: E/AndroidRuntime(30127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 03-26 12:35:16.900: E/AndroidRun
ndSocketRequest(HttpEngine.java:286)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:236)03-26 12:35:16.900: E / AndroidRuntime(30127):位于libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)03-26 12:35:16.900:E / AndroidRuntime(30127):位于libcore.net.http.HttpURLConnectionImpl.getInputStream (HttpURLConnectionImpl.java:168)03-26 12:35:16.900:E / AndroidRuntime(30127):在java.net.URL.openStream(URL.java:462)03-26 12:35:16.900:E / AndroidRuntime (30127):位于nl.appone.artistone.reneevanginkel.Biografie.onCreate(Biografie.java:30)03-26 12:35:16.900:E / AndroidRuntime(30127):位于android.app.Activity.performCreate(Activity。 java:4465)03-26 12:35:16.900:E / AndroidRuntime(30127):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)03-26 12:35:16.900:E / AndroidRuntime(30127) :位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)03-26 12:35:16.900:E / AndroidRun time(30127): ... 11 more 03-26 12:40:16.960: I/Process(30127): Sending signal.
time(30127):... 11 more 03-26 12:40:16.960:I / Process(30127):发送信号 PID: 30127 SIG: 9
PID:30127 SIG:9
What can I do to put the Text from the web into the TextView? 我该怎么做才能将网络上的文本放入TextView? It's a PHP script what have an output with a biography.
这是一个PHP脚本,具有带有传记的输出。
Thanks in Regard! 谢谢Regard!
this will be a better practice, using asyncronous task 使用异步任务,这将是更好的做法
AsyncronousTask atask = new AsyncronousTask();
atask.execute("ur url here");
String res=atask.get();
//Asyncronous taks class goes like
public class AsyncronousTask extends AsyncTask<String, Void, String> {
String response;
Activity c;
//private ProgressDialog dialog;
//String title,msg;
@Override
protected String doInBackground(String... urls) {
response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader( new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
}
use this code, you will get response in return of this method call then you can set that on textView 使用此代码,您将获得响应以返回此方法调用,然后可以在textView上进行设置
public static String HitServerGET(String URL) { 公共静态字符串HitServerGET(String URL){
String result = null;
InputStream is = null;
StringBuilder sb = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
In the onPostExecute, you can set the text in your TextView. 在onPostExecute中,可以在TextView中设置文本。
I re-use the HitServerGET function of Saad Khokhar 我重用了Saad Khokhar的HitServerGET函数
This code can be put in your onCreate. 此代码可以放在您的onCreate中。
final URL url = new URL("http://xxx.nl/api/biografie.php?dataid=998");
new AsyncTask<String, String, String>()
{
String text=null;
@Override
protected String doInBackground(String... params)
{
text = HitServerGET(url)
}
@Override
protected void onPostExecute(String ret)
{
((TextView)findViewById(R.id.biografieText)).setText(text);
}
}.execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.