在另一个类中运行网络操作时获取网络策略违规

时间:2012-06-17 15:24:11

标签: java android http

我正在尝试使用简单的HTTP get来从网页加载字符串。但是,当我在模拟器中运行应用程序时,我得到严格的策略违规,并且不显示任何内容。策略违规列为“policy = 31 violation = 4”。如果我将permitNetwork()添加到ThreadPolicy初始化中,LogCat会告诉我google.com没有解析为某个地址。显然,我错过了一些东西,但我的印象是这就是我应该如何处理网络操作。

编辑:我改变了我的HttpExampleActivity.java,我现在有了这个:

package com.android.httpexample;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy.Builder;
import android.widget.TextView;

public class HttpExampleActivity extends Activity {
    TextView httpStuff;

     @Override
        public void onCreate(Bundle savedInstanceState) {
         StrictMode.ThreadPolicy policy = new Builder().detectAll().penaltyLog().build(); 
         StrictMode.setThreadPolicy(policy);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            new Thread(new Runnable() { 
                public void run() {

                    httpStuff = (TextView) findViewById(R.id.tvHttp);
                    httpStuff.post(new Runnable(){
                        public void run(){

                    GetProcedure test = new GetProcedure();
                    String returned;
                    try{
                        returned = test.getInternetData();
                        httpStuff.setText(returned);
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                        }
                    });
                }
            }).start();


     }
}

在我的GetProcedure.java中,我有这个:

package com.android.httpexample;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetProcedure {
    public String getInternetData() throws Exception {
        BufferedReader in = null;
        String data = null;
        try{
            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://google.com");
            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();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您需要使用执行网络活动的单独线程。自从ICS以来,Android不允许在UI线程中进行网络连接5秒前。

解决任务的一个好方法是使用AsyncTask处理每个人或一个带有侦听器的线程。

查看Painless Threading主题以获取更多建议。