意识到我不得不担心基于网络的线程在我的UI线程上造成破坏,我经历了为我的TCP / IP网络创建自定义处理程序和自定义线程的整个过程。远程服务器。工作得很好。 (您将在下面的代码中看到它被引用为mainCommunicationThread(controlCommands);
)
在触发该线程/处理程序之前,我从SQL中提取IP或主机名,(称之为currIPstr)然后将其与一堆其他变量一起发送到线程/处理程序。 我需要做的一件事是确保它是A)一个有效的IP地址,或者B)主机名被解析 - 否则,即使开始尝试也没有意义。
好的,没问题,我只是这样称呼:
currIP = InetAddress.getByName(currIPstr).toString().split("/")[1];
我刚刚遇到的是,当Gingerbread或StrictMode运行时,上面的行触发了可怕的NetworkOnMainThreadException。
是的,对InetAddress.getByName的简单调用就足够了。好吧,我一直在努力解决这个问题,所以我将整个功能移到了一个可运行的地方:
private String currStatIP = null;
private String currentStatIPstr = null;
private Integer currentStatIDstr = -1;
private String currentPort = null;
private String currentLocation = null;
private String currUserName = null;
private String currPassword = null;
private Integer currStatID = -1;
public void StatControl(Cursor c, final String[] commandtosend){
/*the object will always start off with the first 4 fields being the current login information.
* [0] = IP Address
* [1] = port number
* [2] = username
* [3] = password
* [4] = COMMAND -- this will be used in a switchcase inside the pingpong to decide what to do.
* -- if the Socket is not open, the first 4 fields will be used to re-initiate the connection
* -- otherwise, the command is drawn from this field and sent directly.
* */
currentStatIDstr = c.getInt(0);
currentStatIPstr = c.getString(1);
currentPort = c.getString(2);
currentLocation = c.getString(3);
currUserName = c.getString(4);
currPassword = c.getString(5);
currStatID = currentStatIDstr;
Handler networkLookupHandler = new Handler();
Runnable networkLookupRunnable = new Runnable() {
public void run() {
try {
currStatIP = InetAddress.getByName(currentStatIPstr).toString().split("/")[1];
} catch (UnknownHostException e) {
e.printStackTrace();
}
int portNumber = 0;
try {
portNumber = Integer.parseInt(currentPort);
} catch(NumberFormatException nfe) {
nfe.printStackTrace();
}
String userName = currUserName;
String passWord = currPassword;
String[] command = commandtosend;
Object[] controlCommands = new Object[]{
currStatID,
currStatIP, //InetAddress of String representing the IP address
portNumber, //int representation of the port number, taken from String
currentLocation,
userName,
passWord,
command
};
/*for(int i=0;i<controlCommands.length;i++){
Log.d("object work","controlCommands[" + i + "] is " + controlCommands[i]);
}*/
mainCommunicationThread(controlCommands);
}
};
networkLookupHandler.post(networkLookupRunnable);
}
而且,是的!有效!我不再收到NetworkOnMainThreadException了。 哦......等等..是的。
以上不会在API 10,12,14上触发异常,即使16在模拟器中工作正常,但是,如果我将其加载到运行Jellybean 4.1.1的Samsung Google Galaxy Nexus上,我会收到NetworkOnMainThreadException崩溃。即使上面包裹着runnable。
我完全不知道如何解决这个问题。
答案 0 :(得分:4)
您正试图在runnable中运行网络操作,因此尝试在onCreate方法中添加此代码:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
或者我建议您使用asynctask而不是使用Runnable线程
答案 1 :(得分:3)
我的解决方案:
public static InetAddress getInetAddressByName(String name)
{
AsyncTask<String, Void, InetAddress> task = new AsyncTask<String, Void, InetAddress>()
{
@Override
protected InetAddress doInBackground(String... params)
{
try
{
return InetAddress.getByName(params[0]);
}
catch (UnknownHostException e)
{
return null;
}
}
};
try
{
return task.execute(name).get();
}
catch (InterruptedException e)
{
return null;
}
catch (ExecutionException e)
{
return null;
}
}
答案 2 :(得分:0)
我通过在新线程中运行它解决了我的Inet6Address.getLocalHost()NetworkOnMainThreadException问题。