开发者社区> 问答> 正文

关于Android 开发中,访问网络的问题

public class MainActivity extends Activity {

private EditText address;
private Button getbutton;
private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //版本4.0后需加这个,不然就报错android.os.NetworkOnMainThreadException
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());
        //
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        //初始化

        address = (EditText) findViewById(R.id.address);
        getbutton = (Button) findViewById(R.id.getbutton);
        text = (TextView) findViewById(R.id.text);



        getbutton.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String url = address.getText().toString();
                getPDAServerData(url);
            }
        });

    }

    public void getPDAServerData(String url) {
        HttpClient client = new DefaultHttpClient();
        HttpPost request;

        try {

            request = new HttpPost(url);

            //调用HttpClient对象的execute(HttpUriRequest request)发送请求,返回一个HttpResponse
            HttpResponse response = client.execute(request);

            //返回响应码为200
            if (response.getStatusLine().getStatusCode() == 200) {

                //从响应中获取消息实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String out = EntityUtils.toString(entity);
                    text.setText(out);
                }
            }


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

请问,

 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());

这段代码的意思是什么呢,为什么不加,就无法执行访问网络的代码语句呢。

展开
收起
爵霸 2016-03-10 13:49:43 2232 0
1 条回答
写回答
取消 提交回答
  • request = new HttpPost(url);
    //调用HttpClient对象的execute(HttpUriRequest request)发送请求,返回一个HttpResponse
    HttpResponse response = client.execute(request);

    这部分是网络操作,必须在子线程执行,没有这段代码就会抛出android.os.NetworkOnMainThreadException;
    而这个StrictMode主要是用来调试程序的,这段代码可以检测到android.os.NetworkOnMainThreadException
    异常并发出警告 penaltyLog()表示将警告输出到LogCat。
    StrictMode 用法:http://www.2cto.com/kf/201503/381354.html

    2019-07-17 18:57:29
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载