public class Main2Activity extends AppCompatActivity {
ServerSocket serverSocket;
int localPort;
NsdManager.RegistrationListener registrationListener;
String serviceName = "ef";
NsdManager nsdManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
initializeServerSocket();
initializeRegistrationListener();
registerService(localPort);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@SuppressWarnings("deprecation")
public void registerService (int port) throws UnknownHostException {
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName("testchat");
serviceInfo.setServiceType("nsdchat._tcp");
serviceInfo.setPort(port);
/*WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
int ipAdress = wm.getConnectionInfo().getIpAddress();
serviceInfo.setHost(InetAddress.getByName(Formatter.formatIpAddress(ipAdress)));*/
nsdManager = (NsdManager) this.getSystemService(Context.NSD_SERVICE);
Log.d("nsdmanager", "info: " + nsdManager.toString());
nsdManager.registerService(
serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener);
}
public void initializeServerSocket() throws IOException {
// Initialize a server socket on the next available port.
serverSocket = new ServerSocket(0);
// Store the chosen port.
localPort = serverSocket.getLocalPort();
Log.d("test", "initialized server socket: " + localPort);
}
public void initializeRegistrationListener() {
registrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
// Save the service name. Android may have changed it in order to
// resolve a conflict, so update the name you initially requested
// with the name Android actually used.
Log.d("test", "service registered");
serviceName = NsdServiceInfo.getServiceName();
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Registration failed! Put debugging code here to determine why.
Log.d("test", "registration failed");
Log.d("serviceinfo", serviceInfo.toString());
switch (errorCode) {
case NsdManager.FAILURE_ALREADY_ACTIVE:
Log.d("test", "FAILURE_ALREADY_ACTIVE");
break;
case NsdManager.FAILURE_INTERNAL_ERROR:
Log.d("test", "FAILURE_INTERNAL_ERROR");
break;
case NsdManager.FAILURE_MAX_LIMIT:
Log.d("test", "FAILURE_MAX_LIMIT");
break;
}
}
@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
// Service has been unregistered. This only happens when you call
// NsdManager.unregisterService() and pass in this listener.
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Unregistration failed. Put debugging code here to determine why.
}
};
Log.d("test", "initialized registration listener");
}
以上是我的目标。我想在我的对象中创建一个方法来访问对象中的键并将值放在v1数组中。如何在aV方法中从对象访问键?
谢谢!
以下是显示代码:
06-06 14:23:57.016 16319-16319/xxx D/test: initialized server socket: 49575
06-06 14:23:57.016 16319-16319/xxx D/test: initialized registration listener
06-06 14:23:57.020 16319-16319/xxx D/nsdmanager: info: android.net.nsd.NsdManager@a7f4c86
06-06 14:23:57.030 16319-16457/xxx D/test: registration failed
06-06 14:23:57.031 16319-16457/xxx D/serviceinfo: name: testchat, type: nsdchat._tcp, host: null, port: 49575, txtRecord:
06-06 14:23:57.031 16319-16457/xxx D/test: FAILURE_INTERNAL_ERROR
答案 0 :(得分:0)
您是否需要从此对象访问密钥?如果是这样的话,那么:
var myParser = {
instr: document.getElementById("pString"),
v1: [],
crstr: function() {
return this.instr.value;
},
aV: function() {
for(i=0;i<this.instr.lenght;i++)
if(this.instr[i] == '(') this.v1.push(0);
return this.v1;
},
getKeys:function(){
this.v1 = Object.keys(this)
}}
myParser.getKeys()
console.log(myParser.v1)
如果您要提供另一个对象,则:
var myParser = {
instr: document.getElementById("pString"),
v1: [],
crstr: function() {
return this.instr.value;
},
aV: function() {
for(i=0;i<this.instr.lenght;i++)
if(this.instr[i] == '(') this.v1.push(0);
return this.v1;
},
getKeys:function(obj){
this.v1 = Object.keys(obj)
}}
var foo = {a:2,b:3}
myParser.getKeys(foo)
console.log(myParser.v1)
编辑:我仍然不确定你要做什么,但要修复的两件事是长度而不是lenght,而“this.instr”是HTML元素,this.instr。 value是字符串。
var myParser = {
instr: document.getElementById("pString"),
v1: [],
crstr: function() {
return this.instr.value;
},
aV: function() {
var str = this.instr.value;
for(i=0;i<str.length;i++) {
if(str[i] == '(') this.v1.push(0);
}
return this.v1;
}
}
console.log(myParser.aV())
<input id="pString" value="(abc)">