iteye_12540 2011-12-23 11:09
浏览 223
已采纳

PhoneGAP如何访问网络?

PhoneGAP中我无法找到网络访问相关的API。我不知道该如何通过PhoneGAP访问网络。从PhoneGAP API中唯一看到和网络有关的只有一个检查当前网络状态的API。

  • 写回答

3条回答 默认 最新

  • jkxydp 2011-12-23 18:10
    关注

    那就编写java代码来自己提供接口啊,编写一个phonegap插件,将它注册到xml/plugins.xml里面,具体参照这个里面的注册方式,其中
    [code="java"][/code]这是一个插件的注册
    下面是插件代码

    [code="java"]package com.pingan.market.plugin;

    import java.io.File;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.util.Log;

    import com.phonegap.api.Plugin;
    import com.phonegap.api.PluginResult;
    import com.phonegap.api.PluginResult.Status;

    public class PinganGapPlugin extends Plugin {

    public static final String ACTION = "list";
    
    @Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
        Log.d("DirectoryListPlugin", "Plugin Called");
        PluginResult result = null;
        if (ACTION.equals(action)) {
            try {
                String fileName = data.getString(0);
                JSONObject fileInfo = getDirectoryListing(new File(fileName));
                Log
                .d("DirectoryListPlugin", "Returning "
                + fileInfo.toString());
                result = new PluginResult(Status.OK, fileInfo);
            } catch (JSONException jsonEx) {
                Log.d("DirectoryListPlugin", "Got JSON Exception "
                + jsonEx.getMessage());
                result = new PluginResult(Status.JSON_EXCEPTION);
            }
        } else {
            result = new PluginResult(Status.INVALID_ACTION);
            Log.d("DirectoryListPlugin", "Invalid action : " + action  + " passed");
        }
        return result;
    }
    
    private JSONObject getDirectoryListing(File file) throws JSONException {
        JSONObject fileInfo = new JSONObject();
        fileInfo.put("filename", file.getName());
        fileInfo.put("isdir", file.isDirectory());
        if (file.isDirectory()) {
            JSONArray children = new JSONArray();
            fileInfo.put("children", children);
            if (null != file.listFiles()) {
                for (File child : file.listFiles()) {
    
                    children.put(getDirectoryListing(child));
                }
            }
        }
        return fileInfo;
    }
    

    }[/code]

    在phonegap.js中
    使用PhoneGap.addPlugin函数对接口进行注册 其中name是DirectoryListPlugin,也就是xml里面的name属性,其它的你参照phonegap.js中它的插件注册方式就可以。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮