我正在尝试通过http请求从google获取数据:
主要

public class MainActivity extends ActionBarActivity {
TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpexample);
    httpStuff = (TextView) findViewById(R.id.httpText);
    HTTP test = new HTTP();
    String returned = null;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
    } catch (Exception e) {com
        e.printStackTrace();
    }
}
}

http clas如下:
public class HTTP {

public String getInternetData() throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.google.net");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while((l = in.readLine()) !=null){
            sb.append(l + nl );
        }
        in.close();
        data = sb.toString();
        return data;
    }finally {
        if (in != null){
            try{
                in.close();
                return data;

            }catch (Exception  e){
                e.printStackTrace();
            }
        }
    }
}

}

这是控制台日志:
04-27 19:31:58.063 2167-2191/com.example.briesanji.test d/openglrenderer:请求呈现脏区域:true
04-27 19:31:58.065 2167-2167/com.example.briesanji.test d/:host connection::get()新主机连接已建立0xA687C600,tid 2167
04-27 19:31:58.103 2167-2167/com.example.briesanji.test d/atlas:正在验证映射…
04-27 19:31:58.196 2167-2191/com.example.briesanji.test d/:host connection::get()新主机连接已建立0xA687C7C0,tid 2191
04-27 19:31:58.207 2167-2191/com.example.briesanji.test i/openglrenderer:初始化的egl,版本1.4
04-27 19:31:58.283 2167-2191/com.example.briesanji.test d/openglrenderer:启用调试模式0
04-27 19:31:58.334 2167-2191/com.example.briesanji.test w/egl_emulation:eglsurfaceattrib未实现
04-27 19:31:58.334 2167-2191/com.example.briesanji.test w/openglrenderer:未能在表面0xA6868780上设置egl_swap_行为,错误=egl_success
textView不显示数据,但保留其默认文本(“加载数据”)
我试图用httpStuff.setText(returned);更改httpStuff.setText("hello");,但什么都没有,所以我认为这是布局问题,然后我从try方法中取出httpStuff.setText("hello");,它显示hello。所以问题当然是尝试的方法,但我不知道我错在哪里

最佳答案

您的代码无法工作,因为试图在其主线程上执行网络操作。因此,您将收到NetworkOnMainThreadException
解决此问题的最简单方法是使用AsyncTask

public class SampleActivity extends Activity {

    TextView httpStuff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        httpStuff = (TextView) findViewById(R.id.add_button);
        new DownloadInternetData().execute();
    }

    public class DownloadInternetData extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... voids) {
            BufferedReader in = null;
            String data = null;
            try {
                HttpClient client = new DefaultHttpClient();
                URI website = new URI("http://www.google.net");
                HttpGet request = new HttpGet();
                request.setURI(website);
                HttpResponse response = client.execute(request);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String l = "";
                String nl = System.getProperty("line.separator");
                while ((l = in.readLine()) != null) {
                    sb.append(l + nl);
                }
                in.close();
                data = sb.toString();
                return data;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                        return data;

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            httpStuff.setText(result);
        }
    }
}

09-11 18:00