NetworkOnMainThreadException

NetworkOnMainThreadException

本文介绍了NetworkOnMainThreadException从网络读取时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试阅读网站单行文本的我得到错误(NetworkOnMainThreadException)。我trye​​d一些事情,但没有工作至今。因此,这里是code,如果有人可以提供帮助。在清单中,我有权限互联网让thath不应该是问题。

 进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;

进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;

进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;

公共类天气延伸活动{

    Button按钮;
    TextView的吨;
    字符串结果;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.weather);

        T =(TextView中)findViewById(R.id.textView1);

    }

    公共无效myBu​​ttonClickHandler(查看视图)抛出ClientProtocolException,IOException异常{
        结果= getContentFromUrl(http://url.com);
        t.se​​tText(结果);
    }

    公共静态字符串getContentFromUrl(字符串URL)抛出ClientProtocolException,IOException异常{

        HttpClient的HttpClient的=新DefaultHttpClient();
        HTTPGET HTTPGET =新HTTPGET(URL);
        HTT presponse响应;

        响应= httpClient.execute(HTTPGET);
        HttpEntity实体= response.getEntity();

        如果(实体!= NULL){

            的InputStream inStream中= entity.getContent();

            字符串结果= Weather.convertStreamToString(inStream中);
            inStream.close();

            返回结果;
        }

        返回null;

    }

    私有静态字符串convertStreamToString(InputStream的是){
        的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
        StringBuilder的SB =新的StringBuilder();

        串线= NULL;

        尝试 {
            而((行= reader.readLine())!= NULL){
                sb.append(行+\ N);
            }
        }赶上(IOException异常E){
            e.printStackTrace();
        } 最后 {
            尝试 {
                is.close();
            }赶上(IOException异常E){
                e.printStackTrace();
            }
        }
        返回sb.toString();
    }
}
 

解决方案

不使用主thread.This异常的网络活动仅抛出针对蜂窝SDK或更高版本的应用程序。执行所有的网络在单独的线程(无论是使用处理器弯针或runOnUiThread)相关的任务。您的问题将得到解决。

When i try to read single line of text from website i get error (NetworkOnMainThreadException). I tryed few thing but nothing works so far. So here is code if anyone could help. In manifest i have permission for internet so thath shouldnt be problem.

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();
    }
}
解决方案

don't use network activities in main thread.This exception only thrown for applications targeting the Honeycomb SDK or higher. perform all the network related task in separate thread ( either use handler looper or runOnUiThread) . your problem will get solved.

这篇关于NetworkOnMainThreadException从网络读取时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:02