Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

android - NetworkOnMainThreadException When reading from web

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 + "
");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...