没有从我的自定义Cordova插件(Android)获得回调

时间:2014-08-12 15:28:01

标签: java javascript android cordova

我为多部分调用创建了一个插件,我的插件似乎有用, 但是当我这样称呼它时:

multipart.uploadFiles(function() {
        alert("success");
    }, function() {
        alert("errorr");
    },  wsUrl, strArray, myRequest);  

即使我从我的java代码中打印出正确的响应,我也没有得到任何回调。

如何解决?

这是我的js界面:

var multipart = {

        uploadFiles: function (successCallback, errorCallback, serviceUrl, arrayRes, requestObject) {
            cordova.exec(successCallback,
                    errorCallback, 
                    "Multipart",  // java class, service
                    "uploadFiles", // action
                    [{                 // and this array of custom arguments to create our entry
                        "serviceUrl":serviceUrl,
                        "resPaths": arrayRes,
                        "requestObject": requestObject
                    }]
            );
        }
};

这是我的java代码:

public class Multipart extends CordovaPlugin{

    private static final String MULTIPART_TAG = null;


    @Override
    public boolean execute(final String action, final JSONArray args,
            final CallbackContext callbackContext) throws JSONException {

        final Context context = this.cordova.getActivity();

        // sincronizza il modulo definito in action
        cordova.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    uploadFiles(context, action, args, callbackContext);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }


        });
        return true;
    }

    private static void uploadFiles(Context context, String action, final JSONArray args, CallbackContext callbackContext) throws JSONException {
        String requestURL = null;
        JSONArray resPaths = null;
        JSONObject requestObject = null;
        String charset = "UTF-8";
        JSONObject argObject = args.getJSONObject(0);

         requestURL = argObject.getString("serviceUrl");
         resPaths = argObject.getJSONArray("resPaths");

         requestObject = argObject.getJSONObject("requestObject");
         String request = requestObject.toString();

        try {

            MultipartUtility multipart = new MultipartUtility(requestURL, charset);

            multipart.addHeaderField("User-Agent", "CodeJava");
            multipart.addHeaderField("Test-Header", "Header-Value");

            multipart.addFormField("description", "descr");
            multipart.addFormField("keywords", "Java,upload");
            //multipart.addFormField("request", request);
            multipart.addJsonField("request", request);
         for (int i = 0; i < resPaths.length(); i++) {
                 File fileToPost = new File(resPaths.get(i).toString());
                 multipart.addFilePart("fileUpload", fileToPost);
             }

            List<String> response = multipart.finish();

            System.out.println("SERVER REPLIED:");

            for (String line : response) {
                System.out.println(line);
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

我无法在您的Java代码中看到任何callbackContect.success或callbackContext.error调用。