0

我有一个 Phonegap/Jquery 移动应用程序,我正在尝试为 Android 打包,但我使用的目标 SDK 不允许 Ajax/跨域请求。简而言之,这些请求适用于最新的 Android 4.0.3 (API 15),但不适用于 Android 2.3.3 (API 10),这是我需要与 Galaxy 平板电脑兼容的。我很难调试这个,因为我看不到实际的错误,因为我被包裹在 phonegap 中并且无法使用 Firebug/Chrome 网络工具。而且我在 developer.android.com 上没有任何运气。我已经尝试了位于http://jquerymobile.com/test/docs/pages/phonegap.html的步骤,包括 $.mobile.allowCrossDomainPages = true; $.support.cors = 真。

这也不起作用。谁能帮帮我,我不知道还有什么可以尝试的。谢谢!

这是代码的快速示例。请注意,它返回 success = true 但遇到“响应不是 XML 元素”错误:

 return $.soapRequest({
        url: url,
        namespace: 'testns',
        returnJson: false,
        shortMethod: 'methodname',
        method: 'longmethodname',
        params: params || {},
        success: function(data) {
          if (data && data.documentElement) {
            // hits this on Android SDK 15
            if (successFn) {
              return successFn(data.documentElement);
            }
          }
          else {
            // hits this on Android SDK 10
            return fail("Response is not an XML element!");
          }
        },
        error: function(str) {
          return fail(str);
        }
4

1 回答 1

0

尝试使用 phonegap 插件编写 httpConnection。它发现了 CrossDomain

httpPlugin.java

package com.android.test;

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

import android.util.Log;

import com.android.connection.HTTPConnect;

public class HttpPlugin extends Plugin {
    public final String ACTION_SEND_MESSAGE = "SendCommand";
    private HTTPConnect httpConnect;

    public HttpPlugin() {
        // TODO Auto-generated constructor stub
        httpConnect = new HTTPConnect();
    }

    @Override
    public PluginResult execute(String action, JSONArray arg1, String callbackId) {
        PluginResult result = new PluginResult(Status.INVALID_ACTION);
        if (action.equals(ACTION_SEND_MESSAGE)) {
            try {
                String message = arg1.getString(0);
                String receiveString  = httpConnect.setURL(message);
                if(receiveString == null){
                    //show error result
                    result = new PluginResult(Status.ERROR,"kakaka");
                }else{
                    Log.v("MAN", "data received");
                    result = new PluginResult(Status.OK);
                }
                result = new PluginResult(Status.OK);
            } catch (JSONException ex) {
                // TODO Auto-generated catch block
                result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage());
            }
        }
        return result;
    }
}

plugin.xml 文件

Httpplugin .js

var Httpplugin = function () {};

Httpplugin.prototype.post = function (message, successCallback, failureCallback) {  
//  navigator.notification.alert("OMG");
    return cordova.exec(successCallback, failureCallback, 'Httpplugin', 'SendCommand', [message]);
};

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("http", new Httpplugin());
});
于 2012-04-13T03:13:54.380 回答