Java 调用phonegap插件时出现JSON错误。

Java 调用phonegap插件时出现JSON错误。,java,android,json,cordova,Java,Android,Json,Cordova,我在开发iphone/ipad应用程序时使用了ios的屏幕截图插件。我现在正在创建应用程序的android版本,并尝试实现插件的android版本 插件的java部分如下所示: package org.apache.cordova; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.cordova.api.Plugin; import org.

我在开发iphone/ipad应用程序时使用了ios的屏幕截图插件。我现在正在创建应用程序的android版本,并尝试实现插件的android版本

插件的java部分如下所示:

package org.apache.cordova;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;

public class Screenshot extends Plugin {
private PluginResult result = null;

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    // starting on ICS, some WebView methods
    // can only be called on UI threads
    super.cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            View view = webView.getRootView();

            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);

            try {
                File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                if (!folder.exists()) {
                    folder.mkdirs();
                }

                File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");

                FileOutputStream fos = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                result = new PluginResult(PluginResult.Status.OK);

            } catch (IOException e) {
                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage());
            }
        }
    });

    // waiting ui thread to finish
    while (this.result == null) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // ignoring exception, since we have to wait
            // ui thread to finish
        }
    }

    return this.result;
}

}
(function() {
/* Get local ref to global PhoneGap/Cordova/cordova object for exec function.
    - This increases the compatibility of the plugin. */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks

/**
 * This class exposes the ability to take a Screenshot to JavaScript
 */
function Screenshot() { }

/**
 * Save the screenshot to the user's Photo Library
 */
Screenshot.prototype.saveScreenshot = function() {
cordovaRef.exec(null, null, "Screenshot", "saveScreenshot", []);
};

    if (!window.plugins) {
        window.plugins = {};
    }
    if (!window.plugins.screenshot) {
        window.plugins.screenshot = new Screenshot();
    }

 })(); /* End of Temporary Scope. */
ERROR: org.json.JSONException: Value undefined of type java.lang.String cannot be converted to JSONArray.
Error: Status=8 Message=JSON error
file:///android_asset/www/cordova-2.0.0.js: Line 938 :  Error: Status=8 Message=JSON error
Error: Status=8 Message=JSON error at file:///android_asset_/www/cordova-2.0.0.js:938
我的Screenshot.js如下所示:

package org.apache.cordova;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;

public class Screenshot extends Plugin {
private PluginResult result = null;

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    // starting on ICS, some WebView methods
    // can only be called on UI threads
    super.cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            View view = webView.getRootView();

            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);

            try {
                File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                if (!folder.exists()) {
                    folder.mkdirs();
                }

                File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");

                FileOutputStream fos = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                result = new PluginResult(PluginResult.Status.OK);

            } catch (IOException e) {
                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage());
            }
        }
    });

    // waiting ui thread to finish
    while (this.result == null) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // ignoring exception, since we have to wait
            // ui thread to finish
        }
    }

    return this.result;
}

}
(function() {
/* Get local ref to global PhoneGap/Cordova/cordova object for exec function.
    - This increases the compatibility of the plugin. */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks

/**
 * This class exposes the ability to take a Screenshot to JavaScript
 */
function Screenshot() { }

/**
 * Save the screenshot to the user's Photo Library
 */
Screenshot.prototype.saveScreenshot = function() {
cordovaRef.exec(null, null, "Screenshot", "saveScreenshot", []);
};

    if (!window.plugins) {
        window.plugins = {};
    }
    if (!window.plugins.screenshot) {
        window.plugins.screenshot = new Screenshot();
    }

 })(); /* End of Temporary Scope. */
ERROR: org.json.JSONException: Value undefined of type java.lang.String cannot be converted to JSONArray.
Error: Status=8 Message=JSON error
file:///android_asset/www/cordova-2.0.0.js: Line 938 :  Error: Status=8 Message=JSON error
Error: Status=8 Message=JSON error at file:///android_asset_/www/cordova-2.0.0.js:938
现在我尝试使用以下代码调用screenshot.js函数:

function takeScreenShot() {
  cordovaRef.exec("Screenshot.saveScreenshot");
}   
然而,我得到的只是JSON错误,我知道我在某处要求将其从java字符串转换为JSON,但我就是不知道如何更改它。好吧,我想这就是问题所在

我的错误如下所示:

package org.apache.cordova;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;

public class Screenshot extends Plugin {
private PluginResult result = null;

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    // starting on ICS, some WebView methods
    // can only be called on UI threads
    super.cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            View view = webView.getRootView();

            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);

            try {
                File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                if (!folder.exists()) {
                    folder.mkdirs();
                }

                File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");

                FileOutputStream fos = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                result = new PluginResult(PluginResult.Status.OK);

            } catch (IOException e) {
                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage());
            }
        }
    });

    // waiting ui thread to finish
    while (this.result == null) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // ignoring exception, since we have to wait
            // ui thread to finish
        }
    }

    return this.result;
}

}
(function() {
/* Get local ref to global PhoneGap/Cordova/cordova object for exec function.
    - This increases the compatibility of the plugin. */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks

/**
 * This class exposes the ability to take a Screenshot to JavaScript
 */
function Screenshot() { }

/**
 * Save the screenshot to the user's Photo Library
 */
Screenshot.prototype.saveScreenshot = function() {
cordovaRef.exec(null, null, "Screenshot", "saveScreenshot", []);
};

    if (!window.plugins) {
        window.plugins = {};
    }
    if (!window.plugins.screenshot) {
        window.plugins.screenshot = new Screenshot();
    }

 })(); /* End of Temporary Scope. */
ERROR: org.json.JSONException: Value undefined of type java.lang.String cannot be converted to JSONArray.
Error: Status=8 Message=JSON error
file:///android_asset/www/cordova-2.0.0.js: Line 938 :  Error: Status=8 Message=JSON error
Error: Status=8 Message=JSON error at file:///android_asset_/www/cordova-2.0.0.js:938
谁能告诉我哪里出了问题吗?

问题由以下人员解决:

西蒙·麦克唐纳提供的答案

可能重复的