Put some logs in the Java things, got an error cause i defined the wrong function name

This commit is contained in:
Michael Bykovski 2017-07-11 16:45:27 +02:00
parent ac5b47b1a0
commit 7c1607448e
3 changed files with 26 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package org.apache.cordova.plugin;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.os.Build; import android.os.Build;
import android.util.Log;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.json.JSONException; import org.json.JSONException;
@ -21,7 +22,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD{
Webserver webserver; Webserver webserver;
public NanoHTTPDWebserver(int port, Webserver webserver) { public NanoHTTPDWebserver(int port, Webserver webserver) {
super("0.0.0.0", port); super(port);
this.webserver = webserver; this.webserver = webserver;
} }
@ -53,6 +54,8 @@ public class NanoHTTPDWebserver extends NanoHTTPD{
@Override @Override
public Response serve(IHTTPSession session) { public Response serve(IHTTPSession session) {
Log.d(this.getClass().getName(), "New request is incoming!");
String requestUUID = UUID.randomUUID().toString(); String requestUUID = UUID.randomUUID().toString();
PluginResult pluginResult = null; PluginResult pluginResult = null;
@ -74,6 +77,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD{
} }
JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID); JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID);
Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString());
Response response = null; Response response = null;
try { try {
response = newFixedLengthResponse( response = newFixedLengthResponse(

View File

@ -1,9 +1,10 @@
package org.apache.cordova.plugin; package org.apache.cordova.plugin;
import android.util.Log;
import org.apache.cordova.*; import org.apache.cordova.*;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
@ -13,8 +14,7 @@ public class Webserver extends CordovaPlugin {
public HashMap<String, Object> responses; public HashMap<String, Object> responses;
public CallbackContext onRequestCallbackContext; public CallbackContext onRequestCallbackContext;
public NanoHTTPDWebserver nanoHTTPDWebserver;
private NanoHTTPDWebserver nanoHTTPDWebserver;
@Override @Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) { public void initialize(CordovaInterface cordova, CordovaWebView webView) {
@ -40,8 +40,8 @@ public class Webserver extends CordovaPlugin {
this.onRequest(args, callbackContext); this.onRequest(args, callbackContext);
return true; return true;
} }
else if ("onResponse".equals(action)) { else if ("sendResponse".equals(action)) {
this.onResponse(args, callbackContext); this.sendResponse(args, callbackContext);
return true; return true;
} }
return false; // Returning false results in a "MethodNotFound" error. return false; // Returning false results in a "MethodNotFound" error.
@ -57,9 +57,18 @@ public class Webserver extends CordovaPlugin {
if (args.length() == 1) { if (args.length() == 1) {
port = args.getInt(0); port = args.getInt(0);
} }
this.nanoHTTPDWebserver = new NanoHTTPDWebserver(port, this); this.nanoHTTPDWebserver = new NanoHTTPDWebserver(port, this);
this.nanoHTTPDWebserver.start(); this.nanoHTTPDWebserver.start();
Log.d(
this.getClass().getName(),
"Server is running on: " +
this.nanoHTTPDWebserver.getHostname() + ":" +
this.nanoHTTPDWebserver.getListeningPort()
);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} }
@ -81,7 +90,8 @@ public class Webserver extends CordovaPlugin {
* @param callbackContext * @param callbackContext
* @throws JSONException * @throws JSONException
*/ */
private void onResponse(JSONArray args, CallbackContext callbackContext) throws JSONException { private void sendResponse(JSONArray args, CallbackContext callbackContext) throws JSONException {
Log.d(this.getClass().getName(), "Got sendResponse: " + args.toString());
this.responses.put(args.getString(0), args.get(1)); this.responses.put(args.getString(0), args.get(1));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} }

View File

@ -1,12 +1,16 @@
var WEBSERVER_CLASS = "Webserver"; var WEBSERVER_CLASS = "Webserver";
exports.start = function(success_callback, error_callback, port) { exports.start = function(success_callback, error_callback, port) {
var params = [];
if (port) {
params.push(port);
}
cordova.exec( cordova.exec(
success_callback, success_callback,
error_callback, error_callback,
WEBSERVER_CLASS, WEBSERVER_CLASS,
"start", "start",
[port] params
); );
}; };