2014-10-16 2 views
0

다음을 사용하려고합니다 : plugin 및 JS에서 자바 결과에 액세스하는 데 문제가 있습니다. 나는 결과를 보여주기 위해 노력하고 내 HTML 파일에서자바에서 JS 반환 결과

public void sendCallback(String action, ServiceInfo info) { 
    JSONObject status = new JSONObject(); 
    try { 
     status.put("action", action); 
     status.put("service", jsonifyService(info)); 
     Log.d("ZeroConf", "Sending result: " + status.toString()); 
     PluginResult result = new PluginResult(PluginResult.Status.OK, 
       status); 
     result.setKeepCallback(true); 
     this.callback.sendPluginResult(result); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

JS

var ZeroConf = { 
watch: function (type, callback) { 
    return exec(function (result) { 
     if (callback) { 
      callback(result); 
     } 

    }, ZeroConf.fail, "ZeroConf", "watch", [type]); 
}};module.exports = ZeroConf; 

: 아래

자바 관련 플러그인에 짧은 버전입니다 아무것도 작동하지 않습니다! 나는 JAVA에 익숙하지 않아서 내 이슈의 출처를 알 수 없다.

샘플 HTML 파일 : 나는 자바에서 결과에 액세스하는 방법을 잘 모릅니다 곳

var app = { 
// Application Constructor 
initialize: function() { 
    this.bindEvents(); 
}, 
bindEvents: function() { 
    document.addEventListener('deviceready', this.onDeviceReady, false); 
}, 
onDeviceReady: function() { 
    app.receivedEvent('deviceready'); 
    ZeroConf.watch('_http._tcp.local.', function(result) { 

이다

alert(result) 
     // do something with the result 


    }); 
}, 
receivedEvent: function(id) { 
    var parentElement = document.getElementById(id); 
    var listeningElement = parentElement.querySelector('.listening'); 
    var receivedElement = parentElement.querySelector('.received'); 
    listeningElement.setAttribute('style', 'display:none;'); 
    receivedElement.setAttribute('style', 'display:block;'); 
    console.log('Received Event: ' + id); 
}}; 

주 : 임 있는지의 작업 내가 볼 수 있기 때문에 자바 측 "Log.d"의 결과

답변

0

당신은 object을 결과. 당신은 당신이 그것을하고 싶은 것에 따라 그것을 파싱하거나 현시화할 필요가있다.

onDeviceReady: function() { 
    app.receivedEvent('deviceready'); 
    ZeroConf.watch('_http._tcp.local.', function(result) { 
     // Stringify the result to display it all as a string 
     resultText = JSON.stringify(result); 
     alert(resultText); 

     // just display some values from the object 
     // for example PORT and NAME 
     alert("port: "+result.service.port + ", name: "+result.service.name); 

    }); 
}, 

결과 개체 구조는 plugin's page에 설명되어 있습니다.

+0

자바와 플러그인을 구현할 때 문제가 있다고 의심됩니다. 결과가 리턴되지 않으므로 결과의 값을 인쇄 할 때 정의되지 않은 값을 얻습니다. –

+0

이 플러그인을 유지 관리하고있어 문제에 대한 자세한 정보를 얻고 싶습니다. cordova-cli를 사용하여 앱을 설치 했습니까? 어떤 Android 버전과 기기 유형을 사용하고 있습니까? –