问题描述
我正在尝试使用简单的HTTP get来从网页加载字符串。但是,当我在模拟器中运行应用程序时,我得到严格的策略违规,并且不显示任何内容。策略违规列为policy = 31 violation = 4。如果我将permitNetwork()添加到ThreadPolicy初始化中,LogCat会告诉我google.com没有解析为某个地址。很明显,我错过了一些东西,但我的印象是我应该如何处理网络操作。
I'm trying to use a simple HTTP get to load a string from a webpage. However, when I run the app in the emulator, I get a strict policy violation, and nothing is displayed. The policy violation is listed as "policy=31 violation=4". If I add a permitNetwork() into the ThreadPolicy initialization, the LogCat tells me that google.com doesn't resolve to an address. Clearly, I'm missing something, but I was under the impression that this is how I should be handling network operations.
编辑:我改变了我的HttpExampleActivity.java ,我现在有这个:
I've altered my HttpExampleActivity.java, I now have this:
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,我有这个:
In my GetProcedure.java, I have this:
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();
}
}
}
}
}
推荐答案
您需要使用执行网络活动的单独线程。自从ICS以来,Android不允许在UI线程中建立网络,并且在大约一段时间之后确实发生了关闭。 5秒之前。
You need to use a seperate Thread that executes the network activities. Android doesn't allow networking in the UI-thread since ICS and did raiss a force close after approx. 5 seconds before.
解决任务的一个好方法是使用AsyncTask来处理每个人或者一个带有侦听器的线程。
A good way to solve your task would be an AsyncTask that handles everying or a Thread with a listener for your Activity.
查看主题了解更多信息建议。
Check the Painless Threading Topic for more advice.
这篇关于在另一个类中运行网络操作时获取网络策略违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!