可能重复: Strange NetworkOnMainThreadException在Android应用程序? Trying上传到Dropbox的:NetworkOnMainThreadException
我用下面$ C $下读取URL的HTML内容。这完全适用于2.3.3,但是当我尝试运行相同的code它ICS不起作用。
我想就这些追加的HTML内容到的EditText
。但是,它始终保持空当我运行ICS的code。可能是什么问题?
公共类Quiz1Activity延伸活动{
私有静态的BufferedReader读卡器= NULL;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
的EditText ED =(EditText上)findViewById(R.id.editText1);
尝试 {
ed.append(getStringFromUrl(http://www.google.com));
// getInputStreamFromUrl()关闭()。
}赶上(例外五){
// TODO自动生成的catch块
e.printStackTrace();
}
}
公共静态的InputStream getInputStreamFromUrl(字符串URL){
InputStream的contentStream = NULL;
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse响应= httpclient.execute(新HTTPGET(URL));
contentStream = response.getEntity()的getContent()。
}赶上(例外五){
e.printStackTrace();
}
的System.out.println(内容流为+ contentStream);
返回contentStream;
}
公共静态字符串getStringFromUrl(字符串URL)抛出IOException异常{
读者=新的BufferedReader(新的InputStreamReader(getInputStreamFromUrl(URL)));
StringBuilder的SB =新的StringBuilder();
尝试{
串线= NULL;
而((行= reader.readLine())!= NULL)
{
sb.append(线);
}
}赶上(IOException异常E){
e.printStackTrace();
}
getInputStreamFromUrl(URL).close();
返回sb.toString();
}
}
解决方案
就像@Vipul沙阿说你必须移动getInputStreamFromUrl()到另一个线程使用异步任务,这是对ICS的工作:
包com.home.anas;
进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.widget.EditText;
公共类WebPageContentActivity延伸活动{
私人的EditText编辑;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
ED =(EditText上)findViewById(R.id.editText1);
readWebpage();
}
私有类DownloadWebPageTask扩展的AsyncTask<字符串,太虚,字符串> {
@覆盖
保护字符串doInBackground(字符串...网址){
串响应=;
对于(字符串网址:网址){
DefaultHttpClient客户端=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
尝试 {
HTT presponse执行= client.execute(HTTPGET);
InputStream的内容= execute.getEntity()的getContent()。
的BufferedReader缓冲=新的BufferedReader(新InputStreamReader的(内容));
字符串s =;
而((S = buffer.readLine())!= NULL){
响应+ = S;
}
}赶上(例外五){
e.printStackTrace();
}
}
返回响应;
}
@覆盖
保护无效onPostExecute(字符串结果){
ed.setText(结果);
}
}
公共无效readWebpage(){
DownloadWebPageTask任务=新DownloadWebPageTask();
task.execute(新的String [] {http://www.google.com});
}
}
Possible Duplicate: Strange NetworkOnMainThreadException in Android app? Trying To Upload To Dropbox: NetworkOnMainThreadException?
I have used the below code for reading HTML contents from a url. This works perfectly for 2.3.3 but when I try to run the same code it doesn't work for ICS.
I am trying to append these html contents on to a edittext
. But it always remains empty when I run the code on ICS. What may be the problem?
public class Quiz1Activity extends Activity {
private static BufferedReader reader = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText ed = (EditText) findViewById(R.id.editText1);
try {
ed.append(getStringFromUrl("http://www.google.com"));
//getInputStreamFromUrl("").close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static InputStream getInputStreamFromUrl(String url){
InputStream contentStream = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
contentStream = response.getEntity().getContent();
} catch(Exception e){
e.printStackTrace();
}
System.out.println("Content stream is " + contentStream);
return contentStream;
}
public static String getStringFromUrl(String url) throws IOException{
reader = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));
StringBuilder sb = new StringBuilder();
try{
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line);
}
}catch (IOException e){
e.printStackTrace();
}
getInputStreamFromUrl(url).close();
return sb.toString();
}
}
解决方案
Like @Vipul Shah said you have to move getInputStreamFromUrl() into another thread use Async Task, this is work on ICS:
package com.home.anas;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.EditText;
public class WebPageContentActivity extends Activity {
private EditText ed;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed = (EditText) findViewById(R.id.editText1);
readWebpage();
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String 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;
}
@Override
protected void onPostExecute(String result) {
ed.setText(result);
}
}
public void readWebpage() {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.google.com" });
}
}