每当我使用registerService函数时,它都会出现代码错误为0的异常.
1)我正在创建一个本地聊天服务应用程序,在那里我需要进行本地服务广播,而这样做我一直被困住,无论我做什么,注册总是失败,错误代码为0.
我在onResume()或onPause()块中没有多个注册实例.
2)Logcat只显示其他内容
Attempted to finish an input event but the input event receiver has already been disposed.
每当我点击右上角的注册服务按钮.
这是我的代码:
public String mServicename = "nearByDevices";NsdServiceInfo mService;/** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroIDStudio for more information. */private Googleapiclient clIEnt; public voID registerService(int port) { // Create the NsdServiceInfo object, and populate it. // Cancel any prevIoUs registration request initializeRegistrationListener(); NsdServiceInfo serviceInfo = new NsdServiceInfo(); serviceInfo.setServicename(mServicename); serviceInfo.setServiceType(SERVICE_TYPE); serviceInfo.setPort(port); NsdHelper(this); mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE); mNsdManager.registerService(serviceInfo,NsdManager.PROTOCol_DNS_SD, mRegistrationListener);}public voID NsdHelper(Context context) { mContext = context; mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);}public voID initializeRegistrationListener() { mRegistrationListener = new NsdManager.RegistrationListener() { @OverrIDe public voID onServiceRegistered(NsdServiceInfo NsdServiceInfo) { mServicename = NsdServiceInfo.getServicename(); Toast.makeText(MainActivity.this, "Service registered:" + mServicename, Toast.LENGTH_LONG).show(); } @OverrIDe public voID onRegistrationFailed(NsdServiceInfo arg0, int arg1) { Toast.makeText(MainActivity.this, "Service registration Failed:" + arg1, Toast.LENGTH_LONG).show(); } @OverrIDe public voID onServiceUnregistered(NsdServiceInfo arg0) { Toast.makeText(MainActivity.this, "Service unregistered:" + arg0.getServicename(), Toast.LENGTH_LONG).show(); } @OverrIDe public voID onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { Toast.makeText(MainActivity.this, "Service unregistration Failed:" + errorCode, Toast.LENGTH_LONG).show(); } };}public voID initializediscoveryListener() { // Instantiate a new discoveryListener mdiscoveryListener = new NsdManager.discoveryListener() { // Called as soon as service discovery begins. @OverrIDe public voID ondiscoveryStarted(String regType) { Toast.makeText(MainActivity.this, "Service discovery Started", Toast.LENGTH_LONG).show(); } @OverrIDe public voID onServiceFound(NsdServiceInfo service) { // A service was found! Do something with it. Toast.makeText(MainActivity.this, "Service discovery Success:" + service.getServicename(), Toast.LENGTH_LONG).show(); if (!service.getServiceType().equals(SERVICE_TYPE)) { // Service type is the string containing the protocol and // transport layer for this service. Toast.makeText(MainActivity.this, "UnkNown Service Type" + service.getServiceType(), Toast.LENGTH_LONG).show(); } else if (service.getServicename().equals(mServicename)) { Toast.makeText(MainActivity.this, "Same Machine" + mServicename, Toast.LENGTH_LONG).show(); } else { devices.add((service.getServicename()).toString()); } } @OverrIDe public voID onServiceLost(NsdServiceInfo service) { Toast.makeText(MainActivity.this, "Service Lost:" + service.getServicename(), Toast.LENGTH_LONG).show(); if (mService == service) { mService = null; } } @OverrIDe public voID ondiscoveryStopped(String serviceType) { Toast.makeText(MainActivity.this, "discovery Stopped" + serviceType, Toast.LENGTH_LONG).show(); } @OverrIDe public voID onStartdiscoveryFailed(String serviceType, int errorCode) { Toast.makeText(MainActivity.this, "discovery Failed. Error Code" + errorCode, Toast.LENGTH_LONG).show(); } @OverrIDe public voID onStopdiscoveryFailed(String serviceType, int errorCode) { Toast.makeText(MainActivity.this, "discovery Failed. Error Code" + errorCode, Toast.LENGTH_LONG).show(); } };} public voID initializeResolveListener() { mResolveListener = new NsdManager.ResolveListener() { @OverrIDe public voID onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { // Called when the resolve fails. Use the error code to deBUG. Log.e(TAG, "Resolve Failed" + errorCode); } @OverrIDe public voID onServiceResolved(NsdServiceInfo serviceInfo) { Log.e(TAG, "Resolve Succeeded. " + serviceInfo); if (serviceInfo.getServicename().equals(mServicename)) { Log.d(TAG, "Same IP."); return; } mService = serviceInfo; int port = mService.getPort(); InetAddress host = mService.getHost(); } };} public voID discoverServices() { stopdiscovery(); // Cancel any existing discovery request initializediscoveryListener(); mNsdManager.discoverServices( SERVICE_TYPE, NsdManager.PROTOCol_DNS_SD, mdiscoveryListener);}public voID stopdiscovery() { if (mdiscoveryListener != null) { try { mNsdManager.stopServicediscovery(mdiscoveryListener); } finally { } mdiscoveryListener = null; }} public NsdServiceInfo getChosenServiceInfo() { return mService;}public voID tearDown() { if (mRegistrationListener != null) { try { mNsdManager.unregisterService(mRegistrationListener); } finally { } mRegistrationListener = null; }}@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); //initializeServerSocket(); devices = new ArrayList<String>(); ArrayAdapter adapter = new ArrayAdapter(this, R.layout.main_ListvIEw, devices); ListVIEw ListVIEw = (ListVIEw) findVIEwByID(R.ID.device_List); if(devices.isEmpty()) { devices.add("No Devices Nearby"); ListVIEw.setAdapter(adapter); } else ListVIEw.setAdapter(adapter); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroIDStudio for more information. clIEnt = new Googleapiclient.Builder(this).addAPI(AppIndex.API).build();}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroIDManifest.xml. int ID = item.getItemID(); //noinspection SimplifiableIfStatement switch (ID) { case R.ID.register: // initializeServerSocket(); registerService(65010); return true; case R.ID.discover: discoverServices(); return true; default: return super.onoptionsItemSelected(item); }}@OverrIDepublic voID onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroIDStudio for more information. clIEnt.connect(); Action vIEwAction = Action.newAction( Action.TYPE_VIEW, // Todo: choose an action type. "Main Page", // Todo: define a Title for the content shown. // Todo: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), // Todo: Make sure this auto-generated app deep link URI is correct. Uri.parse("androID-app://com.example.devesh1.mynewapp/http/host/path") ); AppIndex.AppIndexAPI.start(clIEnt, vIEwAction);}@OverrIDepublic voID onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroIDStudio for more information. Action vIEwAction = Action.newAction( Action.TYPE_VIEW, // Todo: choose an action type. "Main Page", // Todo: define a Title for the content shown. // Todo: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), // Todo: Make sure this auto-generated app deep link URI is correct. Uri.parse("androID-app://com.example.devesh1.mynewapp/http/host/path") ); AppIndex.AppIndexAPI.end(clIEnt, vIEwAction); clIEnt.disconnect();}}
解决方法:
我在尝试使用NsdManager注册服务时遇到了这个问题.最后我发现服务类型必须遵循NsdManager documentation中作为示例描述的格式.为了澄清,格式为:“_ name._communicationprotocol”例如:“_ ipp._tcp”和“_http._tcp.
如果没有,则会导致回调onRegistrationFailed,errorCode = 0
BTW.来自NsdManager的调试日志不会从您的应用程序输出到Logcat.您必须使用“No Filters”查看日志才能查看NSD服务的调试信息
更新:再次偶然发现此错误.正在搜索_name._tcp.local但是我应该只搜索_name._tcp
总结以上是内存溢出为你收集整理的android – 使用网络服务发现时注册失败,错误代码为0.全部内容,希望文章能够帮你解决android – 使用网络服务发现时注册失败,错误代码为0.所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)