3

当我尝试从网站读取单行文本时,出现错误(NetworkOnMainThreadException)。我尝试了几件事,但到目前为止没有任何效果。因此,如果有人可以提供帮助,这里是代码。在清单中,我有权访问互联网,所以这不应该是问题。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Weather extends Activity {

    Button button;
    TextView t;
    String result;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.weather);

        t = (TextView)findViewById(R.id.textView1);

    }   

    public void myButtonClickHandler (View view) throws ClientProtocolException, IOException {
        result = getContentFromUrl("http://url.com");
        t.setText(result);
    }

    public static String getContentFromUrl(String url) throws ClientProtocolException, IOException {

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response;

        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();

        if(entity != null) {

            InputStream inStream = entity.getContent();

            String result = Weather.convertStreamToString(inStream);
            inStream.close();

            return result;
        }

        return null;

    }

    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;

        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
4

3 回答 3

8

不要在主线程中使用网络活动。此异常仅针对针对 Honeycomb SDK 或更高版本的应用程序引发。在单独的线程中执行所有与网络相关的任务(使用处理程序循环器或 runOnUiThread)。你的问题会得到解决。

于 2012-02-25T08:53:27.660 回答
5

您可以将其移动到后台线程,或者如果您只想消除此错误,只需将其添加到您的 onCreate() 中:

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 

不要忘记在清单中添加互联网权限:

<uses-permission android:name="android.permission.INTERNET"/>
于 2013-08-27T03:53:49.953 回答
1

您可能希望使用 AsyncTask 完成所有下载,http://developer.android.com/reference/android/os/AsyncTask.html

或者,如果您使用 api 11+ AsyncTaskLoader,http://developer.android.com/reference/android/content/AsyncTaskLoader.html

于 2012-02-25T09:42:25.757 回答